How to clearInterval with unknown ID?

前端 未结 4 1777
眼角桃花
眼角桃花 2020-11-29 04:30

Say someone (evil) has set us a timer with setInterval, but we don\'t know its ID (we don\'t have the reference to the object, that setInterval is returning, no

4条回答
  •  再見小時候
    2020-11-29 05:31

    Well, empirically trials in Chrome show that setInterval returns a number which increments for each call. So if you are SURE that you setInterval was the last one set the following would work :

    function clearLastInterval () {
      var i = setInterval (function () {}, 10000);
      clearInterval (i-1);
      clearInterval (i);
    }
    

    I'm not sure I would recommend this though ;-)

提交回复
热议问题