Answer the question
In order to leave comments, you need to log in
How to execute synchronously loop in nodeJS?
Hello, need to iterate in order, how to achieve this in nodeJS?
There is a code:
for(var i = 1; i <= 10; i++) {
setTimeout(function(){
console.log('Индекс: ' + i);
}, 5000 / i);
}
Answer the question
In order to leave comments, you need to log in
Something like this :)
for(var i = 0; i <= 10; i++) {
console.log('Индекс: ' + i);
}
for(var i = 0; i <= 10; i++) {
(function(i) {
setTimeout(function(){
console.log('Индекс: ' + i);
}, 5000 / i);
})(i);
}
var func = function (i) {
return function () {
console.log('Индекс: ' + i);
};
};
for(var i = 0; i <= 10; i++) {
setTimeout(func(i), 5000 / i);
}
Read up on variable scoping and, if you like, closures in Javascript. This will completely help you understand where critical errors are made in your code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question