- Mastering JavaScript
- Ved Antani
- 91字
- 2021-07-16 09:31:31
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.
推薦閱讀
- Mastering Ext JS(Second Edition)
- Python編程自學(xué)手冊(cè)
- DevOps for Networking
- Visual Basic程序設(shè)計(jì)教程
- Visual C++串口通信技術(shù)詳解(第2版)
- Learn Programming in Python with Cody Jackson
- PostgreSQL Replication(Second Edition)
- Java項(xiàng)目實(shí)戰(zhàn)精編
- Modern JavaScript Applications
- SQL經(jīng)典實(shí)例(第2版)
- iOS開(kāi)發(fā)實(shí)戰(zhàn):從入門到上架App Store(第2版) (移動(dòng)開(kāi)發(fā)叢書(shū))
- Visual Basic程序設(shè)計(jì)習(xí)題與上機(jī)實(shí)踐
- RESTful Web Clients:基于超媒體的可復(fù)用客戶端
- 從程序員角度學(xué)習(xí)數(shù)據(jù)庫(kù)技術(shù)(藍(lán)橋杯軟件大賽培訓(xùn)教材-Java方向)
- C語(yǔ)言從入門到精通