Disclaimer: This article is a blogger original article, follow the CC 4.0 BY copyright agreement, reproduced, please attach the original source link and this statement.
setTimeout
AndsetInterval
the same syntax. They have two parameters, a string of code to be executed, there is a time interval in milliseconds.
setInterval(()=>{}, 1000);
setTimeout(()=>{}, 1000);
- Difference:
setInterval
After performing a code after a specified time interval, execute code, butsetTimeout
only once that code. - Note: Suppose
setTimeout
timer specified time of 1 second, performs a function of time is 2 seconds, thesetTimeout
total running total duration is 3 seconds. ThesetInterval
function will not be called bound, it simply repeats at regular intervals to perform a specified function. Therefore, more complex logic functions, the processing time is longer,setInterval
there may cause problems in successive interference. To avoid this problem, it is recommended throughsetTimeout
to simulate asetInterval
.
achieve:
// 可避免setInterval因执行时间导致的间隔执行时间不一致
setTimeout (function () {
// do something
setTimeout (arguments.callee, 500)
}, 500)
----- END -----
like this article friends, welcome public attention No. Zhang Peiyue , watch for more exciting content! ! ! No public reply to e-books , e-books give you a classic!