在Ionic页面内重复调用REST API

3
在Ionic页面中,我需要显示一个二维码,直到与服务器的rest调用返回确认值为止。为了检查“激活完成”阶段,我需要每一秒或五秒钟进行一次调用。如何在进入页面时启动重复调用并持续执行?
1个回答

0

你可以在 onInit() 函数中开始调用 API

ngOnInit(){    
   this.pingAPI().subscribe(
     (data) => {
        // activate QR code
     }
   ); 
}

private pingAPI(){
         return Observable.timer(1000, 5000)          // after 1 second, tick every 5 second
                    .map(() => {
                        return callAPI().map(
                               (data) => {
                                  // manipulate data
                                  return data;
                               }
                        );
                    })
                    // do not go further unless the property 'activationDone' is truthy
                    .filter(data=> data.activationDone == true)
                    .first();  // stop the chain when condition satisfies for the first time
}

如果我的“callAPI()”是一个Observable<T>,我应该如何编写map? - Mauro Destro
就像你编写map一样,在这里也没有什么不同。更新的答案。 - Amit Chigadani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接