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

  • Mastering JavaScript
  • Ved Antani
  • 91字
  • 2021-07-16 09:31:31

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.

主站蜘蛛池模板: 柯坪县| 定安县| 崇明县| 土默特右旗| 永泰县| 吴旗县| 石家庄市| 华宁县| 定西市| 宜兰县| 洛浦县| 白水县| 香格里拉县| 河间市| 杨浦区| 茂名市| 晋州市| 日土县| 垫江县| 日土县| 湖南省| 宝山区| 称多县| 白水县| 德州市| 宁波市| 中阳县| 习水县| 祁门县| 隆化县| 伊川县| 耿马| 宿迁市| 乃东县| 武宁县| 泾川县| 西华县| 如东县| 大姚县| 腾冲县| 和平区|