我有以下代码片段:
if (someCondition) { // clear globTimer first?? globTimer = setInterval(function() { someBlinkingCode; },1000); } else { clearInterval(globTimer); }
但是这部分代码可以被多次调用,其中someCondition将成立.这意味着将创建多个间隔,并且不会销毁所有间隔.经过一段时间后,闪烁频率超过1秒,所以我添加了clearInterval(globTimer);而不是评论.这个改变解决了我的问题,但这个@R_301_463@案好吗?是否可以为同一个变量调用clearInterval()更多次,或者为undefined调用它?
解决方法
是的,可以使用“无效”标识符调用clearInterval(和clearTimeout).
The specification说:
The clearInterval() method must clear the entry identified as handle from the list of active intervals of the WindowTimers object on which the method was invoked,where handle is the argument passed to the method,if any. (If handle does not identify an entry in the list of active intervals of the WindowTimers object on which the method was invoked,the method does nothing.)
(强调我的)