x秒后停止功能角度9

时间:2020-03-09 14:19:06

标签: angular

我想在10000秒内停止某个功能,但这不适用于angular:

setTimeout(() => {
        this
        .ngZone
        .run(() => {
            if (device.name.includes('DeviceName')) {
              this.connectToDevice(device)
            }
        });
       }, 10000,  console.log('not found'));

这将在10000之后启动该功能,但是我需要在10000秒后停止该功能并显示一条消息。

1 个答案:

答案 0 :(得分:5)

您可以将管道与rxjs中的超时一起使用

从'rxjs / operators'导入{超时};

示例:

your_subscription
  .pipe(timeout(10000)) // closed after 10 seconds
  .subscribe((result) => {
    // do stuff
  });