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

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

主站蜘蛛池模板: 昌宁县| 桐梓县| 嘉义市| 旺苍县| 崇明县| 陇西县| 宁安市| 重庆市| 南汇区| 武夷山市| 玛沁县| 油尖旺区| 黑水县| 盘锦市| 政和县| 邓州市| 隆安县| 宝兴县| 项城市| 贵溪市| 汶上县| 理塘县| 通城县| 抚顺县| 绥棱县| 北票市| 灵璧县| 沁阳市| 克东县| 临潭县| 阳朔县| 临汾市| 浦县| 隆德县| 福泉市| 明水县| 康定县| 柳林县| 金川县| 家居| 铁力市|