Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
}
like image 754
user1032531 Avatar asked Jan 23 '26 07:01

user1032531


2 Answers

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);
like image 193
Quentin Avatar answered Jan 25 '26 20:01

Quentin


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

like image 24
T.J. Crowder Avatar answered Jan 25 '26 21:01

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!