第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 setInterval() 的 HTTP 輪詢(xún)每 1 秒調(diào)用一次,而不是提到的間隔

使用 setInterval() 的 HTTP 輪詢(xún)每 1 秒調(diào)用一次,而不是提到的間隔

波斯汪 2022-10-27 15:57:38
我的 Ionic 4 應(yīng)用程序有一個(gè)要求,我需要每 20 秒調(diào)用一次 API。當(dāng)我使用setInterval()它時(shí),API 每 1 秒而不是 20 秒命中一次。這是我的代碼,我可以知道出了什么問(wèn)題嗎?我的.ts文件getApiData(){  this.http.get('https://kairavforex.com/api/libor_rate/',{},{'Content-Type': 'application/json','Authorization': "Token" + " " +  this.authToken})    .then(data=>{      this.getData=JSON.parse(data.data).results;          })  this.repeatInterval();}repeatInterval(){   this.rateTimer=setInterval(() => {      this.getApiData();   }, 20000);   }
查看完整描述

3 回答

?
胡說(shuō)叔叔

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以嘗試使用 RxJS和(或根據(jù)您的要求)運(yùn)算符來(lái)連續(xù)輪詢(xún)端點(diǎn),而不是依賴(lài)setInterval()or函數(shù)。嘗試以下setTimeout()repeatdelaytakeUntiltakeWhile

一些服務(wù)


stopPolling$ = new Subject();


getApiData(): Observable<any> {

  return this.http.get(

    'https://kairavforex.com/api/libor_rate/',

    {},

    {'Content-Type': 'application/json','Authorization': "Token" + " " +  this.authToken}

  ).pipe(

    tap(data => this.getData = JSON.parse(data.data).results),

    delay(20000),               // <-- poll frequency

    repeat(),                   // <-- poll till `stopPolling$` emits

    takeUntil(stopPolling$)     // <-- emit `stopPolling$` to stop polling

  );

}


stopPollingApi() {

  this.stopPolling$.next();

  this.stopPolling$.complete();

}

一些組件


ngOnInit() {

  this.someService.getApiData().subscribe(    // <-- will start polling

    res => { },

    err => { }

  );

}


someOtherFunc() {               // <-- call this function to stop polling

  if(someCondition) {

    this.someService.stopPollingApi();

  }

}


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊

我通過(guò)在開(kāi)始新間隔之前清除 SetInterval 解決了這個(gè)問(wèn)題,以避免間隔重復(fù)。


getApiData(){

    this.http.get('https://kairavforex.com/api/libor_rate/',{},{'Content-Type': 'application/json','Authorization': "Token" + " " +  this.authToken})

      .then(data=>{

       this.getData=JSON.parse(data.data).results;      

      })

      this.repeatInterval();

  }


  

  repeatInterval(){

    clearInterval(this.rateTimer);

    this.rateTimer=setInterval(() => { 

      this.getApiData(); 

   }, 20000); 

  }


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

在repeatInterval 中調(diào)用getApiData 并將repeatInterval 設(shè)為IIFE


getApiData(){

    this.http.get('https://kairavforex.com/api/libor_rate/',{},{'Content-Type': 'application/json','Authorization': "Token" + " " +  this.authToken})

      .then(data=>{

       this.getData=JSON.parse(data.data).results;      

      })

  }


(repeatInterval(){

     this.rateTimer=setInterval(() => { 

       this.getApiData(); 

    }, 20000);   

  })();


查看完整回答
反對(duì) 回復(fù) 2022-10-27
  • 3 回答
  • 0 關(guān)注
  • 122 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)