tags:

views:

317

answers:

4

i want to know main difference between

setInterval

vs

setTimeout

in javascript?

+2  A: 

SetInterval repeats the call, SetTimeout only runs it once.

Daniel Earwicker
thanks for the info
Pranay Rana
+12  A: 

setInterval fires again and again in intervals, while setTimeout only fires once.

References here: https://developer.mozilla.org/en/DOM/window#Methods

deceze
thanks for the info
Pranay Rana
+6  A: 

setTimeout(expression, timeout); runs the code/function once after the timeout setInterval(expression, timeout); runs the code/function in intervals, with the length of the timeout between them

example:

var intervalID = setInterval(alert, 1000); // will alert every second
// clearInterval(intervalID); // will clear the timer

setTimeout(alert, 1000); // will alert once, after a second.
lunixbochs
thanks for the info
Pranay Rana
+3  A: 

setTimeout():

It is a function that execute javascript statement AFTER x interval.

setTimeout(“do.something();”, 1000); //Execute do.something() 1 second later.

setInterval():

It is a function that execute javascript statement EVERY x interval.

setInterval(“do.somethingElse();”, 2000); //Execute do.somethingElse() every 2 seconds.

The interval unit is in millisecond for both function.

MAS1
thanks for the info
Pranay Rana