Angular2 setTimeout返回ZoneTask而不是"Number"

Aje*_*jey 6 javascript settimeout angular

试图在angular2中使用setTimeout,我想稍后清除超时.

但是Angular2正在返回一个"ZoneTask"而不是一个数字

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

  }
Run Code Online (Sandbox Code Playgroud)

我如何使用正常的setTimeout或什么是首选使用setTimeout然后在Angular2中使用clearTimeout?

Plunkr在这里

yur*_*zui 7

更新

Relase zone@0.8.18(2017-09-27)

timer:修复#314,setTimeout/interval应该返回原来的timerId(#894)(aec4bd4)

以前的版本

你需要打电话

clearTimeout(this.timeoutId);
Run Code Online (Sandbox Code Playgroud)

timeoutId您可以通过调用得到

this.timeoutId.data.handleId
Run Code Online (Sandbox Code Playgroud)

如果你想使用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
Run Code Online (Sandbox Code Playgroud)

但我不明白你为什么这么想