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
source
share