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

  • 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.

主站蜘蛛池模板: 西乌| 临洮县| 太康县| 元谋县| 屏山县| 巩留县| 阿合奇县| 绥德县| 县级市| 青河县| 长治市| 泽普县| 咸阳市| 西畴县| 常山县| 绍兴市| 南开区| 泰来县| 张家川| 杭州市| 民乐县| 洪洞县| 惠东县| 库车县| 蒙城县| 富阳市| 崇义县| 班玛县| 庆元县| 温州市| 浮梁县| 富民县| 山丹县| 宜兰县| 隆林| 普洱| 喀喇| 延安市| 叶城县| 汉寿县| 若尔盖县|