JavaScript > Timer

setInterval vs setTimeout in JavaScript

Difference between setInterval and setTimeout in JavaScript.


setInterval() function

  • Set interval is an method in JavaScript {setInterval() method}, which is used to call a function or evaluates an expression at specified intervals.
  • The specified intervals of time should be in milliseconds.
  • This method executes the function repeatedly.
  • Executes repeatedly means, for example we have given interval time 2 seconds to the function, onclick of the button gives alert after 2 seconds, if we do not click "OK" button inside the alert box and waited upto 20 seconds, we can find 10 alerts continuosly, which are jammed one after the other.
  • Click setInterval() method, to know more about this.

setTimeout() function

  • Set Timeout is an method in JavaScript { setTimeout() method}, which is used to call a function or evaluate an expression after a specified intervals.
  • The specified intervals of time should be in milliseconds.
  • This method  executes the function once.
  • Executes once means, for example we have given interval time 2 seconds to the function, onclick of the button gives alert after 2 seconds. If we want new alert, first we need to close the alert displayed on the window and again onclick of the button on the home page gives the new alert after 2 seconds.
  • Click setTimeout() method, to know more about this.
 Views: 4893 | Post Order: 191



Write for us






Hosting Recommendations