i want to know main difference between
setInterval
vs
setTimeout
in javascript?
i want to know main difference between
setInterval
vs
setTimeout
in javascript?
SetInterval repeats the call, SetTimeout only runs it once.
setInterval fires again and again in intervals, while setTimeout only fires once.
References here: https://developer.mozilla.org/en/DOM/window#Methods
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.
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.