- 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.
- Visual Basic 6.0程序設計計算機組裝與維修
- Clojure for Domain:specific Languages
- Mastering RStudio:Develop,Communicate,and Collaborate with R
- KnockoutJS Starter
- 深入RabbitMQ
- Learning Python Design Patterns
- JavaCAPS基礎、應用與案例
- RESTful Java Web Services(Second Edition)
- Hadoop大數據分析技術
- Python 3快速入門與實戰
- 數據結構:Python語言描述
- Hands-On Dependency Injection in Go
- ASP.NET本質論
- C# 10核心技術指南
- 劍指大數據:企業級電商數據倉庫項目實戰(精華版)