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

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.

主站蜘蛛池模板: 蒙阴县| 泰州市| 仪征市| 保山市| 丰顺县| 邵阳市| 信丰县| 白河县| 老河口市| 清流县| 宜州市| 湘潭市| 怀宁县| 盐池县| 安义县| 乌鲁木齐市| 昌都县| 武安市| 昌邑市| 澄江县| 霍城县| 本溪市| 绥滨县| 舞钢市| 洛宁县| 资源县| 景泰县| 静海县| 通辽市| 武宁县| 宿松县| 高淳县| 汶上县| 含山县| 呼玛县| 交口县| 台中市| 班戈县| 福安市| 时尚| 张家川|