Trying to use setTimeout in angular2 and I want to clear the timeout later.
But Angular2 is returning a "ZoneTask" rather than 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 do I use the normal setTimeout or what is the preferred to use setTimeout and then clearTimeout in Angular2?
Plunkr here
Trying to use setTimeout in angular2 and I want to clear the timeout later.
But Angular2 is returning a "ZoneTask" rather than 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 do I use the normal setTimeout or what is the preferred to use setTimeout and then clearTimeout in Angular2?
Plunkr here
Share Improve this question asked Mar 14, 2017 at 12:12 AjeyAjey 8,22212 gold badges66 silver badges89 bronze badges 2-
1
Try
clearTimeout(this.timeoutId);
without first argument or you can get id by usingthis.timeoutId.data.handleId
– yurzui Commented Mar 14, 2017 at 12:17 - yes but why am I getting a ZoneTask? How do I do a normal settimeout! – Ajey Commented Mar 14, 2017 at 13:25
1 Answer
Reset to default 7Update
Relase [email protected] (2017-09-27)
timer: fix #314, setTimeout/interval should return original timerId (#894) (aec4bd4)
Previous version
You need to call
clearTimeout(this.timeoutId);
The timeoutId
you can get by calling
this.timeoutId.data.handleId
If you wish to use native setTimeout
then you can leverage something like this:
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
But i don't understand why you wish that
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745428162a4627272.html
评论列表(0条)