官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 雅江县| 宜君县| 卓尼县| 咸阳市| 乌什县| 怀宁县| 新安县| 天门市| 兴仁县| 庄浪县| 广灵县| 江西省| 海安县| 义马市| 淅川县| 义马市| 双城市| 尤溪县| 阜新市| 孙吴县| 原平市| 肥乡县| 泽库县| 钟祥市| 体育| 冕宁县| 舟山市| 偃师市| 丰宁| 资阳市| 永新县| 通海县| 怀安县| 通河县| 高密市| 蓬溪县| 保德县| 云浮市| 探索| 清原| 辽宁省|