官术网_书友最值得收藏!

  • JavaScript:Moving to ES2015
  • Ved Antani Simon Timms Narayan Prusty
  • 167字
  • 2021-07-09 19:07:44

Timers

Timers are used to schedule the execution of a particular callback after a specific delay. There are two primary methods to set up such delayed execution: setTimeout and setInterval. The setTimeout() function is used to schedule the execution of a specific callback after a delay, while setInterval is used to schedule the repeated execution of a callback. The setTimeout function is useful to perform tasks that need to be scheduled such as housekeeping. Consider the following example:

setTimeout(function() {
  console.log("This is just one time delay");
},1000);
var count=0;
var t = setInterval(function() {
  count++;
  console.log(count);
  if (count> 5){
    clearInterval(t);
  }
}, 2000 );

First, we are using setTimeout() to execute a callback (the anonymous function) after a delay of 1,000 ms. This is just a one-time schedule for this callback. We scheduled the repeated execution of the callback using setInterval(). Note that we are assigning the value returned by setInterval() in a variable t—we can use this reference in clearInterval() to clear this schedule.

主站蜘蛛池模板: 达孜县| 中超| 黎平县| 武功县| 崇礼县| 吐鲁番市| 新竹市| 宁安市| 临泉县| 平武县| 蒙自县| 富锦市| 台东市| 清水县| 涞源县| 阿瓦提县| 孝感市| 桂阳县| 诏安县| 海口市| 白水县| 天祝| 梁山县| 饶河县| 大关县| 大同县| 隆化县| 康马县| 达日县| 商河县| 手机| 拜泉县| 南城县| 郁南县| 育儿| 晋宁县| 桓台县| 连云港市| 大埔县| 旌德县| 邯郸市|