最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get setTimeout delay from timeoutID - Stack Overflow

programmeradmin7浏览0评论

How can one find the timeout delay based on the timeoutID (and if not possible by the timeoutID, then perhaps something else such as the timeout object, etc)

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

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

How can one find the timeout delay based on the timeoutID (and if not possible by the timeoutID, then perhaps something else such as the timeout object, etc)

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

function getOriginalTimeoutValue(timeoutId) {
   // find original timeout delay (i.e. 2000)
}
Share Improve this question edited Jul 1, 2020 at 7:50 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 13, 2017 at 13:45 user1032531user1032531 26.4k75 gold badges245 silver badges416 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You can't. The API doesn't expose that information. You have to track it yourself.

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

How can one find the timeout delay based on the timeout ID (and if not possible by the timeout ID, then perhaps something else such as the timeout object, etc

You can't, at all. That information is not provided by any browser API. You could write a wrapper around setTimeout to do it (perhaps even replacing the default one), but it's not something that's available out of the box.

A quick-and-dirty version of your own functions for this might look something like this (I've stuck with ES5-level stuff, so no Map), but be sure to subject it to code review, this is just dashed off:

(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);

Or if you really needed to replace the global functions (not usually a good idea):

(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);

This is just a start (and untested), you might find — at least — that you want to handle clearInterval as well (since it can clear a timer set with setTimeout).

发布评论

评论列表(0)

  1. 暂无评论