Saturday, 19 November 2016

javascript - 'setInterval' vs 'setTimeout'




What is the main difference between



setInterval



and



setTimeout



in JavaScript?



Answer



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.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...