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

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.

主站蜘蛛池模板: 六枝特区| 安岳县| 水富县| 万年县| 延津县| 南召县| 祁门县| 酒泉市| 潮安县| 淮安市| 乐亭县| 新蔡县| 夏河县| 湖南省| 朝阳市| 三亚市| 东乌| 北流市| 苏尼特左旗| 福海县| 贺州市| 汉中市| 溧水县| 隆回县| 双牌县| 金川县| 班玛县| 澜沧| 同心县| 水富县| 孟连| 辉县市| 绥化市| 寻乌县| 枣阳市| 江安县| 临湘市| 竹溪县| 台山市| 万年县| 唐海县|