'setInterval' vs 'setTimeout'

Javascript

Javascript Problem Overview


What is the main difference between

setInterval

and

setTimeout

in JavaScript?

Javascript Solutions


Solution 1 - Javascript

setTimeout(expression, timeout); runs the code/function once after the timeout.

setInterval(expression, timeout); runs the code/function repeatedly, with the length of the timeout between each repeat.

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.

Solution 2 - Javascript

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

See reference at MDN.

Solution 3 - Javascript

setTimeout():

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

setTimeout(function () {
    something();
}, 1000); // Execute something() 1 second later.

setInterval():

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

setInterval(function () {
    somethingElse();
}, 2000); // Execute somethingElse() every 2 seconds.

The interval unit is in millisecond for both functions.

Solution 4 - Javascript

setInterval()

setInterval is a time interval based code execution method that has the native ability to repeatedly run specified script when the interval is reached. It should not be nested into its callback function by the script author to make it loop, since it loops by default. It will keep firing at the interval unless you call clearInterval().

if you want to loop code for animations or clocks Then use setInterval.

function doStuff() {
alert("run your code here when time interval is reached");
}
var myTimer = setInterval(doStuff, 5000);
setTimeout()

setTimeout is a time based code execution method that will execute script only one time when the interval is reached, and not repeat again unless you gear it to loop the script by nesting the setTimeout object inside of the function it calls to run. If geared to loop, it will keep firing at the interval unless you call clearTimeout().

function doStuff() {
alert("run your code here when time interval is reached");
}
var myTimer = setTimeout(doStuff, 5000);

if you want something to happen one time after some seconds Then use setTimeout... because it only executes one time when the interval is reached.

Solution 5 - Javascript

setInterval repeats the call, setTimeout only runs it once.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPranay RanaView Question on Stackoverflow
Solution 1 - JavascriptlunixbochsView Answer on Stackoverflow
Solution 2 - JavascriptdecezeView Answer on Stackoverflow
Solution 3 - JavascriptMAS1View Answer on Stackoverflow
Solution 4 - JavascriptHaris N IView Answer on Stackoverflow
Solution 5 - JavascriptDaniel EarwickerView Answer on Stackoverflow