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

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.

主站蜘蛛池模板: 房产| 安阳县| 繁峙县| 兰西县| 嘉禾县| 庐江县| 高唐县| 微博| 荣成市| 清涧县| 民县| 曲沃县| 额尔古纳市| 温州市| 万荣县| 尉氏县| 鸡西市| 曲水县| 东乡县| 鄂伦春自治旗| 于田县| 靖安县| 勐海县| 漠河县| 孝感市| 泸西县| 黄龙县| 调兵山市| 内江市| 晴隆县| 五寨县| 农安县| 庆安县| 绩溪县| 湖州市| 衡山县| 临朐县| 界首市| 乐平市| 济阳县| 固始县|