admin管理员组文章数量:1429518
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
本文标签: javascriptAngular2 setTimeout returning ZoneTask rather than a quotNumberquotStack Overflow
版权声明:本文标题:javascript - Angular2 setTimeout returning ZoneTask rather than a "Number" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745428161a2658207.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论