【问题标题】:stop function after x seconds angular 9x 秒角 9 后停止功能
【发布时间】:2020-03-09 14:19:06
【问题描述】:

我想在 10000 秒内停止一个函数,但这不适用于 angular:

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

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

【问题讨论】:

标签: angular


【解决方案1】:

你可以在 rxjs 中使用带有超时的管道

从 'rxjs/operators' 导入 { timeout };

示例:

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

【讨论】:

    猜你喜欢
    • 2018-01-28
    • 2019-05-16
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多