- 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.
推薦閱讀
- 黑客攻防從入門到精通(實戰(zhàn)秘笈版)
- ASP.NET MVC4框架揭秘
- Testing with JUnit
- R語言經典實例(原書第2版)
- 軟件項目管理(第2版)
- Twilio Best Practices
- Web程序設計(第二版)
- TradeStation交易應用實踐:量化方法構建贏家策略(原書第2版)
- Keras深度學習實戰(zhàn)
- Learning R for Geospatial Analysis
- Django 3.0入門與實踐
- 奔跑吧 Linux內核
- 會當凌絕頂:Java開發(fā)修行實錄
- iOS Development with Xamarin Cookbook
- RESTful Web API Design with Node.js