我有一个从服务器获取数据的请求。这个请求每秒执行一次。假设我想在console.log("New post")
出现新条目时发送消息。新增一条新记录的请求如何明确?
ngOnInit() {
this.load();
}
load() {
this.ngUnsubscribe = timer(0, 1000).pipe(
switchMap(() => this._orders.getAllOrders())
).subscribe(orders => {
this.orders = orders
}, error => {
this._toast.error(error.error.message);
})
}
抱歉发表评论。在将新值分配给您的变量之前,请检查是否等于
counter = 0;
load() {
this.ngUnsubscribe = timer(0, 1000).pipe(
switchMap(() => this._orders.getAllOrders())
).subscribe(orders => {
if(JSON.stringify(orders) !== JSON.stringify(this.orders)) {
this.orders = orders;
if (this.counter > 0) {
console.log('new data');
}
this.counter++;
}
}, error => {
this._toast.error(error.error.message);
})
}
为防止显示第一个请求的消息,您可以使用计数器:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句