Home >Web Front-end >JS Tutorial >javascript (use setTimeout instead of setInterval)_javascript skills

javascript (use setTimeout instead of setInterval)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:57:59944browse
Copy code The code is as follows:

var num = 0;
var max = 10;
function incrementNumber() {
num ;
//if the max has not been reached, set another timeout
if (num < max) {
setTimeout(incrementNumber, 500);
} else {
alert(“Done”);
}
}
setTimeout(incrementNumber, 500);

If you use setInterval, the next call may occur Called before the previous call (

True intervals are rarely used in production environments because it ' s possible that one interval will begin before the previous one has finished executing)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn