Javascript 检查超时是否已清除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5226578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a timeout has been cleared?
提问by chobo2
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.
当你清除它时,我认为 t 会像 undefined 。但它似乎有一些没有被清除的 id。
回答by Reid
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
回答by jordancpaul
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:
首先,我将这个答案的一部分归功于里德,但我觉得我应该添加一些建议。我对 Reid 的代码稍加添加,这将:
- auto-clear when the timeout naturally expires
- optionally set the scope of the timeout function (rather than just executing in global scope).
- optionally pass arguments array to the timeout function
- 超时自然到期时自动清除
- 可选地设置超时函数的范围(而不仅仅是在全局范围内执行)。
- 可选地将参数数组传递给超时函数
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]
[开始免评论插件]哦,我用的是给类添加方法的原型模型,只是因为我喜欢它,而不是因为我觉得它更正确 [结束免评论插件]
回答by Gavin
Just set t
to 0
(or t in your case) in your timeout function:
只需在超时函数中设置t
为0
(或在您的情况下为 t):
timeoutID = 0;
If you use clearTimeout
it sets the timeout id to 0, so checking for timeoutID === 0
will check if it's either been been cleared or completed.
如果您使用clearTimeout
它,则将超时 ID 设置为 0,因此检查timeoutID === 0
将检查它是否已被清除或已完成。
回答by user113716
No. In order to know, you'll need to null the t
variable after you call clearTimeout
. Otherwise there's really no indicator.
不。为了知道,您需要t
在调用clearTimeout
. 否则真的没有指标。
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);
回答by Jesper Wilfing
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.
就像一些用户在评论中建议的那样,无论何时触发超时(在设置的时间过后)或使用 清除clearTimeout
,手动将其设置为false
或最好设置为null
。然后您可以执行一个简单的 if 检查以查看它是否处于活动状态。还记得将它初始化为false
/null
如果需要。
Created a small test page for this: https://codepen.io/TheJesper/pen/rJzava
为此创建了一个小测试页面:https: //codepen.io/TheJesper/pen/rJzava
回答by Oleg
There is another way to check a presence of the timeout. Value which holds a timeout has for default a number value which is increasing with time. So we can do next in "if" construction:
还有另一种方法可以检查超时是否存在。默认情况下,持有超时的值具有随时间增加的数字值。所以我们可以在“if”构造中做下一步:
if (someBooleanValue && !parseInt(@changeToCriteriaTimeout) > 0){
//Do something
}