Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make it trigger/alert every 5 minutes in 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'
      );
    }
  }

how to make it trigger/alert every 5 minutes. but first it will alert then after the first alert/trigger it will alert/trigger again every 5 minutes.

cause I already set the setInterval like this.

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

but it didn't alert/trigger first.

like image 343
Panda Avatar asked Dec 17 '22 13:12

Panda


2 Answers

You could create your notifications first, once, then set the interval:

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

createNotification();

setInterval(() => {
  createNotification();
}, 300000);

Or, even cleaner, you could use the timer() Observable:

import {timer} from 'rxjs';

// starts immediately, then every 5 minutes
timer(0, 300000).subscribe(() => { 
  this.notification.create(...);
});
like image 132
Guillaume Avatar answered Dec 31 '22 03:12

Guillaume


  1. Put your code in a method.
  2. Call this method.
  3. Additionally, call this method every 5 minutes with an interval.
    createNotification() {
      if (types) {
        ...
      } else {
        ...
      }
    }

    // then in another method : 

    this.createNotification();

    setInterval(() => {
      this.createNotification();
    }, 300000);

    // Or more simply : 

    this.createNotification();

    setInterval(() => this.createNotification.bind(this) , 300000);

like image 36
Jeremy Thille Avatar answered Dec 31 '22 02:12

Jeremy Thille