summary javascript: setTimeout setInterval simulation

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.
This link: https://blog.csdn.net/u012149969/article/details/99616738
  • setTimeoutAnd setIntervalthe 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: setIntervalAfter performing a code after a specified time interval, execute code, but setTimeoutonly once that code.
  • Note: Suppose setTimeouttimer specified time of 1 second, performs a function of time is 2 seconds, the setTimeouttotal running total duration is 3 seconds. The setIntervalfunction 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, setIntervalthere may cause problems in successive interference. To avoid this problem, it is recommended through setTimeoutto simulate a setInterval.
    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!

Guess you like

Origin blog.csdn.net/u012149969/article/details/99616738