If I don't know the return value of setInterval()
or setTimeOut()
, can I still use clearInterveral(id)
or clearTimeOut(id)
to clear them?
Thanks.
You can replace original both setTimeout and setInterval like:
setInterval = (function( oldsetInterval){
var registered=[],
f = function(a,b){
return registered[ registered.length ] = oldsetInterval(a,b)
};
f.clearAll = function(){
var r;
while( r = registered.pop()) {
clearInterval( r );
}
};
return f;
})(window.setInterval);
And now:
setInterval( function(){alert(5000)}, 5000 );
setInterval( function(){alert(10000)}, 10000 );
setInterval.clearAll();
Suggesting a commentary from @PointedEars you shouldn't use the same name so:
reportingSetInterval = as above;
reportingSetInterval( function(){alert(5000)}, 5000 );
and so on..
您可以为此使用基于寄存器模式的对象。
据我所知,没有原始ID是不可能的,因此将其存储在数组中可能是个好主意
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.