I've been working on a project that requires a Timer to make an API call every 10 seconds. I tried using setTimeout, but encountered an issue where it turned into an infinite loop. Even when navigating to another page, it continued to execute the if condition.
Here's an example of how I'm currently implementing the Timer:
In a method, I use the following code to initiate the 10-second API calls:
setTimeout(() => {
this.onTimeOut();
}, 1000);
This is the onTimeOut() method:
onTimeOut() {
this.ApiCall().then(
success => {
if(success ['ok'] == 0){
this.navCtrl.push(myPage);
}
},
error => { console.log(error); });
}
setTimeout(() => {
this.onTimeOut();
}, 1000);
}
I've heard about Debounce and rxjs/rs, but I'm unfamiliar with them. Can you provide some advice on how to achieve the same result using those methods? Alternatively, if the current approach is more efficient, please explain why it turns into a loop.
The objective is to stop the timer once it enters the if condition and pushes the page.