- 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.
推薦閱讀
- Mobile Web Performance Optimization
- OpenCV實(shí)例精解
- Flink SQL與DataStream入門、進(jìn)階與實(shí)戰(zhàn)
- 算法大爆炸:面試通關(guān)步步為營
- Mastering OpenCV 4
- Spring+Spring MVC+MyBatis整合開發(fā)實(shí)戰(zhàn)
- 前端HTML+CSS修煉之道(視頻同步+直播)
- Java EE 8 Application Development
- Spring MVC+MyBatis開發(fā)從入門到項(xiàng)目實(shí)踐(超值版)
- Django 5企業(yè)級(jí)Web應(yīng)用開發(fā)實(shí)戰(zhàn)(視頻教學(xué)版)
- C# 7.0本質(zhì)論
- Mapping with ArcGIS Pro
- Enterprise Application Architecture with .NET Core
- Android熱門應(yīng)用開發(fā)詳解
- Python實(shí)戰(zhàn)指南:手把手教你掌握300個(gè)精彩案例