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

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.

主站蜘蛛池模板: 区。| 永清县| 莫力| 新沂市| 柳林县| 文安县| 桃源县| 林甸县| 伊宁县| 曲靖市| 三穗县| 安阳市| 车致| 资溪县| 中宁县| 松溪县| 县级市| 石棉县| 上栗县| 嘉义县| 惠安县| 长宁县| 忻州市| 吉首市| 元阳县| 张北县| 邵武市| 当涂县| 阳春市| 阿坝| 河北省| 黎城县| 陆川县| 大竹县| 竹溪县| 清新县| 邳州市| 南投县| 沙坪坝区| 临湘市| 麻城市|