从服务器端收到值后停止setinterval

时间:2019-09-08 16:37:25

标签: angular setinterval ionic4 reload

我正在研究离子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)

      }

    })


  } 

3 个答案:

答案 0 :(得分:1)

clearInterval子句应该在interval内,以便定期调用if来调用get:)

答案 1 :(得分:0)

尝试此解决方案。像我一样尝试制作people.jsonset interval
检查解决方案。

clear interval

答案 2 :(得分:0)

http.get是可观察的,不是承诺。您需要订阅,而不使用“ then”。如果您使用httpClient,则它不是必需的“解析器”,数据变为json

顺便说一句,我更喜欢'rxjs'计时器,并使用takeWhile停止,并使用switchMap获取响应

workout