如何使其在角度上每 5 分钟触发/警报一次

Pan*_*nda 4 javascript typescript angular

  limitExceed(params: any) {
    params.forEach((data: any) => {
      if (data.humidity === 100) {
        this.createNotification('warning', data.sensor, false);
      } else if (data.humidity >= 67 && data.humidity <= 99.99) {
        this.createNotification('warning', data.sensor, true);
      }
    });
  }

  createNotification(type: string, title: string, types: boolean): void {
    this.notification.config({
      nzPlacement: 'bottomRight',
      nzDuration: 5000,
    });
    if (types) {
      this.notification.create(
        type,
        title,
        'Humidity reached the minimum limit'
      );
    } else {
      this.notification.create(
        type,
        title,
        'Humidity reached the maximum'
      );
    }
  }
Run Code Online (Sandbox Code Playgroud)

如何让它每 5 分钟触发/警报一次。但首先它会发出警报,然后在第一次警报/触发之后,它会每 5 分钟再次发出警报/触发。

因为我已经这样设置了setInterval

setInterval(() => {
if (types) {
      this.notification.create(
        type,
        title,
        'Humidity reached the minimum limit'
      );
    } else {
      this.notification.create(
        type,
        title,
        'Humidity reached the maximum'
      );
    }
    }, 300000);
Run Code Online (Sandbox Code Playgroud)

但它没有首先提醒/触发。

Gui*_*ume 7

您可以先创建一次通知,然后设置间隔:

function createNotification() {
  this.notification.create(...);
}

createNotification();

setInterval(() => {
  createNotification();
}, 300000);
Run Code Online (Sandbox Code Playgroud)

或者,甚至更干净,您可以使用timer() Observable

import {timer} from 'rxjs';

// starts immediately, then every 5 minutes
timer(0, 300000).subscribe(() => { 
  this.notification.create(...);
});
Run Code Online (Sandbox Code Playgroud)