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

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

主站蜘蛛池模板: 上高县| 永和县| 桓台县| 刚察县| 安徽省| 天镇县| 长武县| 肇庆市| 正蓝旗| 彭山县| 德清县| 香格里拉县| 西贡区| 青州市| 聂拉木县| 大埔县| 苗栗县| 忻州市| 安达市| 关岭| 大石桥市| 湖南省| 沐川县| 岗巴县| 和田市| 忻州市| 库伦旗| 河北省| 景泰县| 奇台县| 贵南县| 宁津县| 桦南县| 廊坊市| 临江市| 康保县| 阳城县| 五家渠市| 绥宁县| 偏关县| 蓬安县|