Answer the question
In order to leave comments, you need to log in
Asynchronous array processing with a timer, why doesn't it work?
Hi all.
There is a need to process an array asynchronously, with a delay.
I made a fiddle that clearly shows the problem:
https://jsfiddle.net/akdes/20he422s/
According to my idea, with zero delay, I should write to the console the text from the first array "foo" and only after 2 seconds "bar"
What I'm doing wrong ? how to solve the problem?
Thanks in advance!
Answer the question
In order to leave comments, you need to log in
Everything is made easier:
var items = [
{
text: "foo",
delay: 0
},
{
text: "bar",
delay: 2000
}];
printAsync();
function printAsync() {
var delay = 0;
items.forEach(item=>setTimeout(function() {
console.log(item.text)
}, item.delay))
}
https://jsfiddle.net/ddo259tv/Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question