Checking whether clearInterval has been called?

前端 未结 2 2026
慢半拍i
慢半拍i 2021-02-01 14:35

Given this code:

bob = setInterval(function, 1000);
clearInterval(bob);

Is there now a way to know if that interval has been cleared?

C

2条回答
  •  忘了有多久
    2021-02-01 15:05

    bob only contains an id of the interval used to clear it. When you call clearInterval, it gets the interval associated with that id and clears it. The id isn't changed at all.

    see here for demonstration

    example:

    
    
    Javascript clearInterval
    
    
    
    

    This will show you the interval's id (returned by setInterval earlier). If you know the interval's id is 1, you can just use clearInterval(1) to clear the interval. So your way of using setting bob to null is a good way of doing it. Just be sure that !bob doesn't return true if the bob happens to be 0. :D

提交回复
热议问题