- JavaScript:Moving to ES2015
- Ved Antani Simon Timms Narayan Prusty
- 91字
- 2021-07-09 19:07:33
Timers and callbacks
In implementing timers or callbacks, you need to call the handler asynchronously, mostly at a later point in time. Due to the asynchronous calls, we need to access variables from outside the scope in such functions. Consider the following example:
function delay(message) { setTimeout( function timerFn(){ console.log( message ); }, 1000 ); } delay( "Hello World" );
We pass the inner timerFn()
function to the built-in library function, setTimeout()
. However, timerFn()
has a scope closure over the scope of delay()
, and hence it can reference the variable message.
推薦閱讀
- Python數據分析入門與實戰
- HTML5+CSS3基礎開發教程(第2版)
- C語言程序設計教程(第2版)
- Mastering PHP Design Patterns
- Silverlight魔幻銀燈
- C語言從入門到精通(第4版)
- Windows Phone 7.5:Building Location-aware Applications
- Android應用案例開發大全(第二版)
- 計算機應用基礎案例教程
- 軟件測試教程
- Couchbase Essentials
- Android傳感器開發與智能設備案例實戰
- Java EE Web應用開發基礎
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)
- AI自動化測試:技術原理、平臺搭建與工程實踐