Javascript 如何使其在角速度下每5分钟触发/警报一次
但它没有首先发出警报/触发。您可以先创建通知,一次,然后设置间隔:Javascript 如何使其在角速度下每5分钟触发/警报一次,javascript,angular,typescript,Javascript,Angular,Typescript,但它没有首先发出警报/触发。您可以先创建通知,一次,然后设置间隔: setInterval(() => { if (types) { this.notification.create( type, title, 'Humidity reached the minimum limit' ); } else { this.notification.create( type,
setInterval(() => {
if (types) {
this.notification.create(
type,
title,
'Humidity reached the minimum limit'
);
} else {
this.notification.create(
type,
title,
'Humidity reached the maximum'
);
}
}, 300000);
或者,甚至更干净,您也可以使用定时器()
可观察的
:
function createNotification() {
this.notification.create(...);
}
createNotification();
setInterval(() => {
createNotification();
}, 300000);
您可以在setInterval内使用立即调用函数:
import {timer} from 'rxjs';
// starts immediately, then every 5 minutes
timer(0, 300000).subscribe(() => {
this.notification.create(...);
});
只需返回函数并放置()
,它将在声明后立即执行一次,然后在实际的setInterval()中执行
一个简单的测试示例:
setInterval(函数通知(){
console.log(“通知”);
退货通知;
}(), 3000);代码>
将代码放入方法中
调用此方法
此外,每隔5分钟调用一次此方法
你可以在设置时间间隔时在第一次通知时直接调用它,也可以使用setTimeout为5min的递归函数。哦,非常好,我从来没有听说过RxJS的timer
RxJS有这么多方法,一旦你习惯了,你可以用它们做什么,比如听文档滚动事件,对它们进行去抖动,每滚动1秒调用一个API。。。太好了:s
import {timer} from 'rxjs';
// starts immediately, then every 5 minutes
timer(0, 300000).subscribe(() => {
this.notification.create(...);
});
setInterval(function notification() {
if (types) {
this.notification.create(
type,
title,
'Humidity reached the minimum limit'
);
} else {
this.notification.create(
type,
title,
'Humidity reached the maximum'
);
}
return notification;
}(), 300000);
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);