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

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.

主站蜘蛛池模板: 高青县| 湘潭县| 策勒县| 聂荣县| 化隆| 乌审旗| 锡林郭勒盟| 望奎县| 边坝县| 敖汉旗| 邢台县| 康保县| 高邑县| 柏乡县| 会理县| 黔江区| 仁怀市| 洪江市| 那坡县| 罗源县| 宝坻区| 得荣县| 元谋县| 镇平县| 沾益县| 抚远县| 扎囊县| 长海县| 乐至县| 铅山县| 察隅县| 南皮县| 桂东县| 容城县| 从化市| 青河县| 霞浦县| 文化| 象山县| 荥阳市| 修武县|