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

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.

主站蜘蛛池模板: 东港市| 邹城市| 东乌珠穆沁旗| 余干县| 遂川县| 醴陵市| 大邑县| 百色市| 云龙县| 甘德县| 孙吴县| 无为县| 通辽市| 湟源县| 赫章县| 永德县| 伊川县| 衢州市| 明水县| 肥西县| 麦盖提县| 确山县| 广州市| 花莲市| 舒兰市| 隆子县| 阿拉善左旗| 大名县| 资源县| 九台市| 五华县| 伊宁县| 宣城市| 沂南县| 即墨市| 图木舒克市| 五寨县| 宾阳县| 石嘴山市| 辉南县| 东阳市|