Angular2 setTimeout returns ZoneTask, not "Number"

Trying to use setTimeout in angular2, and I want to clear the timeout later.

But angular2 returns "ZoneTask", not a number

constructor() {
    this.name = 'Angular2'
    this.timeoutId = setTimeout(() => {  
      console.log('hello');
    }, 2000);


    console.log("timeout ID---", this.timeoutId); // Output - ZoneTask {_zone: Zone, runCount: 0, _zoneDelegates: Array[1], _state: "scheduled", type: "macroTask"…}_state: "notScheduled"_zone: Zone_zoneDelegates: nullcallback: function () {cancelFn: nulldata: Objectinvoke: function () {runCount: 0scheduleFn: function scheduleTask(task) {source: "setTimeout"state: (...)type: "macroTask"zone: (...)__proto__: ZoneTask app.ts:24

  }

How to use regular setTimeout, or what is preferred to use setTimeout and then clearTimeout in Angular2?

Plunkr here

+4
source share
1 answer

Refresh

Relase zone@0.8.18 (2017-09-27)

timer: fix # 314, setTimeout / interval should return the original timerId (# 894) (aec4bd4)

previous version

You need to call

clearTimeout(this.timeoutId);

timeoutId you can get by calling

this.timeoutId.data.handleId

native setTimeout, - :

this.timeoutId = window['__zone_symbol__setTimeout'](() => {  
  console.log('hello');
}, 2000); // setTimeout will work, but it returns ZoneTask

console.log("timeout ID---", this.timeoutId);
window['__zone_symbol__clearTimeout'](this.timeoutId); // clearTimeout will also work

, ,

+5

Source: https://habr.com/ru/post/1672240/


All Articles