I'm trying to place a delay between api calls within a for loop.
I've tried using the setTimeout() without any luck.
Here is the code:
for(let pc = 0; pc < this.cModel.stuff.length; pc++)
{
for(let mc = 0; mc < this.cModel.stuff[pc].otherStuff.length; mc){
let mp = this.mcModel.stuff[pc].otherStuff[mc];
//method that calls api ***need delay***
this.callCancel(mp.key, cp.key);
}
}
private (mpkey: string, cpkey: string){
//code that builds up object *obj
//for api call here
this.executeApiCallHere(obj);
}
Here is what I've tried:
I've tried putting the setTimeout() inside the forloop as such:
setTimeout(this.callCancel(mp.key, cp.key), 1000);I've tried putting the setTimeout() inside the method that calls the api as such:
setTimeout(() => { this.executeApiCallHere(obj);}, 1000);I've tried placing the setTimeout() around the forloop as such:
setTimeout(function () { ......code ....}, 1000);
All of the above result with no delay between calls.
How should I handle this in Typescript so that foreach item in the forloop there is a delay between calls?