I have a asynchronous function that I want to have a 5000ms delay before being fired. I am trying to use setTimeout()
to achieve this. This async function occurs in a loop that runs several times, with the async function being passed different data each time, thus setInterval()
cannot be used here.
Problem: The async function gets triggered instantly without delay (console prints 5 Done
messages instantly` and loops without any delay. What happened, and how can I solve it?
Javascript Code
someFunction(listings, function() {
for (var i in listings ) {
var listing = listings[i];
setTimeout(asyncFunction(listing, function(data) {
console.log('Done');
}), 5000);
}
});