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

  • 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.

主站蜘蛛池模板: 广昌县| 商丘市| 临湘市| 嵊泗县| 沁阳市| 大丰市| 长白| 扎兰屯市| 惠东县| 丹阳市| 北流市| 巴林左旗| 潼关县| 汉源县| 公主岭市| 新竹县| 邵阳县| 岗巴县| 额济纳旗| 厦门市| 双流县| 大城县| 汶上县| 庆城县| 台东市| 双城市| 常州市| 万年县| 历史| 马尔康县| 乐平市| 南京市| 克什克腾旗| 双鸭山市| 婺源县| 天津市| 明水县| 达尔| 大理市| 茂名市| 江山市|