- 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.
- AngularJS入門與進階
- PostgreSQL for Data Architects
- CentOS 7 Server Deployment Cookbook
- Django Design Patterns and Best Practices
- FFmpeg入門詳解:音視頻原理及應用
- Windows Phone 7.5:Building Location-aware Applications
- 深度學習:Java語言實現
- Visual Studio 2015高級編程(第6版)
- 深入理解C指針
- C++從入門到精通(第6版)
- OpenCV Android Programming By Example
- OpenCV Android開發實戰
- Python Deep Learning
- PostgreSQL Developer's Guide
- VMware vSphere Design Essentials