Thinking80s
Thinking80s

Reputation: 2870

setTimeout and setInterval different

setTimeout and setInterval What is the difference between, for example, in the long-running function when the UI process?

  setTimeout(function(){
    /* Some long block of code... */
  }, 10);

  setInterval(function(){
    /* Some long block of code... */
  }, 10);

If there is a long execution time, the execution time is greater than setTimeout or setInterval to set the time

Upvotes: 0

Views: 238

Answers (2)

mrk
mrk

Reputation: 5127

setTimeout runs once and is good to use when you either

  1. only want to run once
  2. or runtime per call is variable and you need to have the call made sequentially

setInterval runs forever until you call clearInterval to cancel.

So, for long running process, it's good to use setTimeout and then have your setTimeout handler call setTimeout again to keep the loop running.

EDIT The problem w/ setInterval is that if it takes longer than 10ms (in your case) to run then that next call can be dropped.

Upvotes: 5

Aurelia
Aurelia

Reputation: 1062

To answer your other question, there is no difference besides the already mentioned one, both get the same priority.

Upvotes: 0

Related Questions