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

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

主站蜘蛛池模板: 白沙| 汕头市| 贵州省| 井陉县| 九龙城区| 临桂县| 新郑市| 左权县| 南澳县| 元朗区| 宁阳县| 淳化县| 蚌埠市| 城口县| 盘锦市| 开封县| 许昌县| 新乡县| 军事| 邹城市| 罗城| 五常市| 万年县| 马龙县| 怀安县| 岢岚县| 苗栗市| 博野县| 南通市| 二连浩特市| 惠水县| 西和县| 湖北省| 石台县| 苏尼特左旗| 庆城县| 周口市| 临猗县| 伊宁县| 西乡县| 晋江市|