【问题标题】:Get setTimeout delay from timeoutID从 timeoutID 获取 setTimeout 延迟
【发布时间】:2017-12-13 13:45:44
【问题描述】:

如何根据timeoutID 找到超时延迟(如果timeoutID 不可能,那么可能还有其他东西,例如超时对象等)

timeoutID = window.setTimeout(slowAlert, 2000);
getOriginalTimeoutValue(timeoutId);

function getOriginalTimeoutValue(timeoutId) {
   // find original timeout delay (i.e. 2000)
}

【问题讨论】:

    标签: javascript dom-events settimeout


    【解决方案1】:

    你不能。 API 不会公开该信息。您必须自己跟踪。

    var timeout_data = {};
    timeout_data.time = 2000;
    timeout_data.id = setTimeout(slowAlert, timeout_data.time);
    

    【讨论】:

    • 谢谢昆汀,是的,会按照您的说明进行跟踪。
    【解决方案2】:

    如何根据超时 ID 找到超时延迟(如果不能通过超时 ID,则可能是其他东西,例如超时对象等

    你根本做不到。该信息不是由任何浏览器 API 提供的。您可以在 setTimeout 周围编写一个包装器来执行此操作(甚至可以替换默认的包装器),但这不是开箱即用的东西。

    你自己的函数的快速和肮脏版本可能看起来像这样(我坚持使用 ES5 级别的东西,所以没有 Map),但一定要接受代码审查,这个只是划掉了:

    (function(global) {
        // For our active timers
        var timers = {};
        // setTimeout wrapper with one that remembers the ID and delay
        global.mySetTimeout = function(callback, delay) {
            // Grab any args passed after delay
            var args = Array.prototype.slice.call(arguments, 2);
            var timer = setTimeout(function() {
                // Forget the ID
                delete timers[timer];
                // Call the callback
                return callback.apply(null, args);
            }, delay);
            timers[timer] = delay;
            return timer;
        };
        // clearTimeout wrapper to delete our record
        global.myClearTimeout = function(timer) {
            delete timers[timer];
            clearTimeout(timer);
        };
        // Create a new global function to get the delay
        global.getTimeoutDelay = function(timer) {
            return timers[timer];
        };
    })(this);
    

    或者如果你真的需要替换全局函数(通常不是一个好主意):

    (function(global) {
        // For our active timers
        var timers = {};
        // The original functions
        var origSetTimeout = setTimeout;
        var origClearTimeout = clearTimeout;
        // Replace setTimeout with one that remembers the ID and delay
        setTimeout = function(callback, delay) {
            // Grab any args passed after delay
            var args = Array.prototype.slice.call(arguments, 2);
            var timer = origSetTimeout(function() {
                // Forget the ID
                delete timers[timer];
                // Call the callback
                return callback.apply(null, args);
            }, delay);
            timers[timer] = delay;
            return timer;
        };
        // Replace clearTimeout to delete our record
        clearTimeout = function(timer) {
            delete timers[timer];
            origClearTimeout(timer);
        };
        // Create a new global function to get the delay
        global.getTimeoutDelay = function(timer) {
            return timers[timer];
        };
    })(this);
    

    只是一个开始(并且未经测试),您可能会发现 - 至少 - 您也想处理 clearInterval(因为它可以清除使用 setTimeout 设置的计时器) .

    【讨论】:

    • 谢谢 TJ。好吧,至少我可以放弃寻找并研究不同的解决方案。
    • 感谢指出mapdeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/…我从未使用过它,但会研究其他用途。
    • 您的两个示例最初是否会使用this.setTimeout(someFunc,2000);this.setTimeout(function(){/*..*/},2000); EDIT 调用。还是只有getTimeoutDelay 公开可用?如果是,它是如何启动的?
    • @user1032531:你根本不会使用this。除此之外,任何一种形式,都取决于您的需要。
    • @user1032531:您使用了上述“您自己的函数”版本,但随后尝试通过setTimeout 调用它。您可以使用这些调用mySetTimeout,或者使用其他的。
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 2012-09-19
    • 2016-11-15
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多