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

  • 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.

主站蜘蛛池模板: 荆州市| 松原市| 巫溪县| 鹤岗市| 景德镇市| 阿拉善左旗| 临洮县| 广灵县| 溧阳市| 黄平县| 贺州市| 四子王旗| 澳门| 呼图壁县| 宜春市| 通化市| 札达县| 瑞安市| 电白县| 富源县| 嘉兴市| 莱阳市| 临湘市| 玛沁县| 康马县| 资阳市| 乌兰察布市| 六枝特区| 忻州市| 武山县| 尖扎县| 江北区| 定远县| 镇雄县| 太康县| 壤塘县| 呼玛县| 会泽县| 杂多县| 达尔| 龙井市|