我正在研究离子4和角铁。我的应用程序符合航班时间表。我想做的是,如果到达特定航班,则从我的服务器接收本地通知。 l使用setInterval
重新加载服务器以检查航班是否到达。问题是航班到达时,setInterval
仍在重新加载,我也使用了clearInterval
但没有停止。
async ActiveNotif() {
this.ng.run(() => {
// reloading
this.interval= setInterval(()=>{
this.http.get('flight=' + this.id + '', {}, {})
.then(data => {
let FlightDetails = JSON.parse(data.data);
this.identification = FlightDetails.identification.callsign;
this.Fstatus = FlightDetails.status.generic.status.text;
console.log(this.Fstatus)
});
//checking
if (this.Fstatus == "landed") {
this.localNotifications.schedule(
[
{
id:0,
title: 'Your flight has arrived',
text: 'Your flight ' + this.identification + 'is arrived',
trigger: { count: 1 },
sound: "file://assets/one.mp3"
},
]);
}
},10000)
if (this.Fstatus == "landed") {
clearInterval(this.interval)
}
})
}
答案 0 :(得分:1)
clearInterval
子句应该在interval
内,以便定期调用if
来调用get
:)
答案 1 :(得分:0)
尝试此解决方案。像我一样尝试制作people.json
和set interval
。
检查解决方案。
clear interval
答案 2 :(得分:0)
http.get是可观察的,不是承诺。您需要订阅,而不使用“ then”。如果您使用httpClient,则它不是必需的“解析器”,数据变为json
顺便说一句,我更喜欢'rxjs'计时器,并使用takeWhile停止,并使用switchMap获取响应
workout