- 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.
推薦閱讀
- Getting Started with Citrix XenApp? 7.6
- Mastering Zabbix(Second Edition)
- Learning Real-time Processing with Spark Streaming
- Vue.js快跑:構(gòu)建觸手可及的高性能Web應(yīng)用
- Building Mapping Applications with QGIS
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- 零基礎(chǔ)趣學(xué)C語(yǔ)言
- Mastering Xamarin.Forms(Second Edition)
- 大學(xué)計(jì)算機(jī)基礎(chǔ)
- MySQL從入門(mén)到精通
- Learning ROS for Robotics Programming
- Learning SaltStack(Second Edition)
- JavaScript高級(jí)程序設(shè)計(jì)(第3版)
- Java從入門(mén)到精通(第7版)
- Learning Embedded Android N Programming