- 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.
推薦閱讀
- 程序員面試筆試寶典(第3版)
- Deploying Node.js
- ThinkPHP 5實(shí)戰(zhàn)
- Python入門很簡單
- JavaScript 網(wǎng)頁編程從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開發(fā)視頻大講堂)
- Unity Virtual Reality Projects
- Python Geospatial Development(Second Edition)
- Learning Network Forensics
- Keras深度學(xué)習(xí)實(shí)戰(zhàn)
- C++從入門到精通(第5版)
- 持續(xù)輕量級(jí)Java EE開發(fā):編寫可測試的代碼
- Unity&VR游戲美術(shù)設(shè)計(jì)實(shí)戰(zhàn)
- OpenMP核心技術(shù)指南
- Python自然語言理解:自然語言理解系統(tǒng)開發(fā)與應(yīng)用實(shí)戰(zhàn)
- 寫給大家看的Midjourney設(shè)計(jì)書