Skip to content Skip to sidebar Skip to footer

Current Time In Setinterval?

setInterval(function() { var current = ? getCurrentInterval ? ; alert(current); }, 2000); Is possible to check?

Solution 1:

There's nothing that will give you the "current" interval, as there might be several distinct timers running. You might be better off constructing your own Timer class that stores the interval and that you can later query.

You're going to have to be a little creative with Javascript's scoping contexts if you have multiple timers and need to access the relevant Timer inside your callback. Something along these lines:

functionTimer(timeout) {
    var self = this;
    this.interval = timeout ? timeout : 1000;   // Defaultthis.run = function (runnable) {
        setInterval(function () { runnable(self); }, this.interval);
    };
}

var timer = newTimer(1000);
timer.run(function (timer) {
    alert(timer.interval);
});

Post a Comment for "Current Time In Setinterval?"