- 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.
- JavaScript全程指南
- JavaScript高效圖形編程
- C++面向對象程序設計(微課版)
- Manga Studio Ex 5 Cookbook
- 精通Scrapy網絡爬蟲
- Interactive Applications Using Matplotlib
- Python語言實用教程
- 零基礎學C語言程序設計
- Kubernetes進階實戰
- RocketMQ實戰與原理解析
- Mastering Gephi Network Visualization
- Java程序設計實用教程(第2版)
- Python面試通關寶典
- Building E-Commerce Solutions with WooCommerce(Second Edition)
- WCF 4.5 Multi-Layer Services Development with Entity Framework(Third Edition)