【问题标题】:Stop setinterval after receiving value from server side从服务器端接收到值后停止 setinterval
【发布时间】:2020-01-10 14:49:07
【问题描述】:

我正在研究 ionic 4 和 angular。我的应用程序是航班时刻表的内容。如果特定航班到达,我想做的是从我的服务器接收本地通知。 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)

      }

    })


  } 

【问题讨论】:

    标签: angular setinterval ionic4 reload


    【解决方案1】:

    clearInterval 子句应该在您的interval 内,以便定期调用if 并得到get 结果:)

    【讨论】:

    • 感谢您的回答。我以前做过,但他还在跑!
    【解决方案2】:

    试试这个解决方案。尝试像我一样制作set intervalclear interval
    查看解决方案。

    async ActiveNotif() {
    
       this.ng.run(() => {
          // reloading 
          var time = setInterval(callingFunction , 10000);
          function callingFunction(){
            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)
    
            });
    
    
    
            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"
                    },
    
    
                    ]);
            }           
          if (this.Fstatus == "landed") {
    
    
            clearInterval(time)
    
          }
          }
       })
    }
    

    【讨论】:

      【解决方案3】:

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

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

      this.dataReaded=false;
      timer(0,1000).pipe(
         takewhile(()=>!this.dataReaded),
         switchMap(()=>{
            return this.httpClient.get('flight=' + this.id + '', {}, {})
         }))
         .subscribe(res=>{
             const FlightDetails = data.data;
             this.identification = FlightDetails.identification.callsign;
             this.Fstatus = FlightDetails.status.generic.status.text;
             this.dataReaded=true;
         })
      

      【讨论】:

      • 感谢您的回答。不幸的是,我正在使用 cordova 插件 http 。 [github.com/silkimen/cordova-plugin-advanced-http],她没有订阅。
      • 抱歉,我看到了“http”,我想那是 Angular 的 httpClient。出于好奇,为什么要使用 http 插件?如果你记得使用 ngZone 来告诉 Angular 一些已经改变。
      • 我使用 http 插件来避免跨域。但是您有其他解决方案吗?
      • 好吧,你可以使用 'rxjs' 运算符 'from' 在 switchMap return from(this.http(....)) 中将 promise 转换为 Observable 但我不确定
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      相关资源
      最近更新 更多