- 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.
- 跟“龍哥”學C語言編程
- Python GUI Programming Cookbook
- Java EE 7 Development with NetBeans 8
- C語言程序設計學習指導與習題解答
- Learning SciPy for Numerical and Scientific Computing(Second Edition)
- Swift 4從零到精通iOS開發
- 工業機器人離線編程
- Vue.js 3應用開發與核心源碼解析
- 小程序從0到1:微信全棧工程師一本通
- ASP.NET Web API Security Essentials
- Hack與HHVM權威指南
- SQL Server實例教程(2008版)
- Microsoft XNA 4.0 Game Development Cookbook
- Building Microservices with Go
- SQL Server 2008數據庫應用技術(第2版)