I am wondering is there a way to tell if a timeout is still set
var t=setTimeout("alertMsg()",3000);
I thought t would be like undefined when you clear it. But it seems to have some id that does not get cleared.
I am wondering is there a way to tell if a timeout is still set
var t=setTimeout("alertMsg()",3000);
I thought t would be like undefined when you clear it. But it seems to have some id that does not get cleared.
Not directly, but you can create a wrapper object to give that functionality. A rough implementation is like so:
function Timeout(fn, interval) {
var id = setTimeout(fn, interval);
this.cleared = false;
this.clear = function () {
this.cleared = true;
clearTimeout(id);
};
}
Then you can do something like:
var t = new Timeout(function () {
alert('this is a test');
}, 5000);
console.log(t.cleared); // false
t.clear();
console.log(t.cleared); // true
First of all, I am giving credit to Reid for portions of this answer, however I felt that I should add some suggestions. With my slight additions to Reid's code, this will:
here it is:
function Timeout(fn, interval, scope, args) {
scope = scope || window;
var self = this;
var wrap = function(){
self.clear();
fn.apply(scope, args || arguments);
}
this.id = setTimeout(wrap, interval);
}
Timeout.prototype.id = null
Timeout.prototype.cleared = false;
Timeout.prototype.clear = function () {
clearTimeout(this.id);
this.cleared = true;
this.id = null;
};
[begin comment-free plug] Oh, and I am using the prototype model of adding methods to classes, but only because I prefer it, not because I feel it is more correct [end comment-free plug]
Just set t
to 0
in your timeout function:
t = 0;
If you use clearTimeout
it sets the timeout id to 0, so checking for t === 0
will check if it's either been cleared or completed.
No. In order to know, you'll need to null the t
variable after you call clearTimeout
. Otherwise there's really no indicator.
And FYI, it's better to pass a direct reference to the function instead of a string that will be eval'd.
var t=setTimeout(alertMsg,3000);
Like some users suggests in comments, whenever the timeout is either triggered (after the set time has passed) or cleared with clearTimeout
, set it manually to false
or preferably null
. Then you can perform a simple if-check to see if it is active. Also remember to initialize it as false
/null
if needed.
Created a small test page for this: https://codepen.io/TheJesper/pen/rJzava
this is only relevant for node.js: setTimeout returns an object, which has the _destroyed property
Welcome to Node.js v18.12.1.
Type ".help" for more information.
> let x = setTimeout(function(){},60000)
undefined
> x
Timeout {
_idleTimeout: 60000,
_idlePrev: [TimersList],
_idleNext: [TimersList],
_idleStart: 2170,
_onTimeout: [Function (anonymous)],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(refed)]: true,
[Symbol(kHasPrimitive)]: false,
[Symbol(asyncId)]: 27,
[Symbol(triggerId)]: 5
}
> x._destroyed
false
> clearTimeout(x)
undefined
> x._destroyed
true
>
note: _destroyed is also true if the event has fired
> let y = setTimeout(function(){console.log("yay");},10)
undefined
> yay
> y
Timeout {
_idleTimeout: 10,
_idlePrev: null,
_idleNext: null,
_idleStart: 198348,
_onTimeout: [Function (anonymous)],
_timerArgs: undefined,
_repeat: null,
_destroyed: true,
[Symbol(refed)]: true,
[Symbol(kHasPrimitive)]: false,
[Symbol(asyncId)]: 508,
[Symbol(triggerId)]: 5
}