setTimeout or setInterval?

前端 未结 19 3031
佛祖请我去吃肉
佛祖请我去吃肉 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:42

    The difference is obvious in console:

    enter image description here

    0 讨论(0)
  • 2020-11-21 05:43

    I think SetInterval and SetTimeout are different. SetInterval executes the block according to the time set while, SetTimeout executes the block of code once.

    Try these set of codes after the timeout countdown seconds:

    setInterval(function(e){
        alert('Ugbana Kelvin');
    }, 2000);
    

    and then try

    setTimeout(function(e){
        alert('Ugbana Kelvin');
    }, 2000);
    

    You can see the differences for yourself.

    0 讨论(0)
  • 2020-11-21 05:46

    If you set the interval in setInterval too short, it may fire before the previous call to the function has been completed. I ran into this problem with a recent browser (Firefox 78). It resulted in the garbage collection not being able to free memory fast enough and built up a huge memory leak. Using setTimeout(function, 500); gave the garbage collection enough time to clean up and keep the memory stable over time.

    Serg Hospodarets mentioned the problem in his answer and I fully agree with his remarks, but he didn't include the memory leak/garbage collection-problem. I experienced some freezing, too, but the memory usage ran up to 4 GB in no time for some minuscule task, which was the real bummer for me. Thus, I think this answer is still beneficial to others in my situation. I would have put it in a comment, but lack the reputation to do so. I hope you don't mind.

    0 讨论(0)
  • 2020-11-21 05:47

    They essentially try to do the same thing, but the setInterval approach will be more accurate than the setTimeout approach, since setTimeout waits 1000ms, runs the function and then sets another timeout. So the wait period is actually a bit more than 1000ms (or a lot more if your function takes a long time to execute).

    Although one might think that setInterval will execute exactly every 1000ms, it is important to note that setInterval will also delay, since JavaScript isn't a multi-threaded language, which means that - if there are other parts of the script running - the interval will have to wait for that to finish.

    In this Fiddle, you can clearly see that the timeout will fall behind, while the interval is almost all the time at almost 1 call/second (which the script is trying to do). If you change the speed variable at the top to something small like 20 (meaning it will try to run 50 times per second), the interval will never quite reach an average of 50 iterations per second.

    The delay is almost always negligible, but if you're programming something really precise, you should go for a self-adjusting timer (which essentially is a timeout-based timer that constantly adjusts itself for the delay it's created)

    0 讨论(0)
  • 2020-11-21 05:47

    I use setTimeout.

    Apparently the difference is setTimeout calls the method once, setInterval calls it repeatdly.

    Here is a good article explaining the difference: Tutorial: JavaScript timers with setTimeout and setInterval

    0 讨论(0)
  • 2020-11-21 05:49

    To look at it a bit differently: setInterval insures that a code is run at every given interval (i.e. 1000ms, or how much you specify) while setTimeout sets the time that it 'waits until' it runs the code. And since it takes extra milliseconds to run the code, it adds up to 1000ms and thus, setTimeout runs again at inexact times (over 1000ms).

    For example, timers/countdowns are not done with setTimeout, they are done with setInterval, to ensure it does not delay and the code runs at the exact given interval.

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