setTimeout or setInterval?

前端 未结 19 3194
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:59

As far as I can tell, these two pieces of javascript behave the same way:

Option A:

function myTimeoutFunction()
{
    doStuff();
           


        
相关标签:
19条回答
  • 2020-11-21 05:49

    Both setInterval and setTimeout return a timer id that you can use to cancel the execution, that is, before the timeouts are triggered. To cancel you call either clearInterval or clearTimeout like this:

    var timeoutId = setTimeout(someFunction, 1000);
    clearTimeout(timeoutId);
    var intervalId = setInterval(someFunction, 1000),
    clearInterval(intervalId);
    

    Also, the timeouts are automatically cancelled when you leave the page or close the browser window.

    0 讨论(0)
提交回复
热议问题