Search code examples
javascriptdom-eventssettimeout

Get setTimeout delay from timeoutID


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)
}

Solution

  • 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).