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