- 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.
推薦閱讀
- 架構(gòu)不再難(全5冊(cè))
- ASP.NET Core 2 and Vue.js
- Object-Oriented JavaScript(Second Edition)
- Windows Server 2012 Unified Remote Access Planning and Deployment
- Android傳感器開發(fā)與智能設(shè)備案例實(shí)戰(zhàn)
- Spring Security Essentials
- Hands-On Neural Network Programming with C#
- Flask Web開發(fā):基于Python的Web應(yīng)用開發(fā)實(shí)戰(zhàn)(第2版)
- 30天學(xué)通C#項(xiàng)目案例開發(fā)
- Get Your Hands Dirty on Clean Architecture
- Learning Alfresco Web Scripts
- Moodle 3.x Developer's Guide
- ASP.NET Core 2 High Performance(Second Edition)
- Puppet Essentials
- 交互設(shè)計(jì)語言:與萬物對(duì)話的藝術(shù)(全兩冊(cè))