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

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.

主站蜘蛛池模板: 黑河市| 嫩江县| 汾阳市| 涡阳县| 广水市| 蒲江县| 那曲县| 石渠县| 兴业县| 新津县| 大厂| 岫岩| 大悟县| 成武县| 托克托县| 沙湾县| 沅江市| 锡林郭勒盟| 宁晋县| 南宁市| 桦川县| 普安县| 德安县| 洪洞县| 乐业县| 富锦市| 海伦市| 河南省| 青川县| 广安市| 青浦区| 日喀则市| 河津市| 延庆县| 延津县| 永兴县| 汝城县| 贵溪市| 邯郸县| 乌兰浩特市| 大洼县|