I'm currently exploring how to implement a timeout for a function within a loop iteration in an Ionic application using TypeScript.
setInterval
will execute the function at equal time intervals in a continuous loop:
setInterval(() => {
this.myFunc1(val);
}, 800);
setTimeout
can achieve the desired outcome when executed sequentially:
setTimeout(() => {
this.myFunc1(val);
}, 800);
setTimeout(() => {
this.myFunc1(val);
}, 1200);
But the challenge lies in creating a loop with time intervals through an updated list, ensuring that each iteration waits for the previous iteration to finish before passing the second value val
to the function or calling myFunc1
:
async myFunc2() {
for (let val of this.myValueList) {
/// Wait for 5 seconds or until the process is complete, then pass the value by calling the function:
this.myFunc1(val);
}
}