Console isn't displaying the correct "people".
My function is as follows:
(function() {
var people, length = 10;
for (people = 0; people < this.length; people++) {
setTimeout(function() {
console.log(people);
}, 1000);
}
})();
Console isn't displaying the correct "people".
My function is as follows:
(function() {
var people, length = 10;
for (people = 0; people < this.length; people++) {
setTimeout(function() {
console.log(people);
}, 1000);
}
})();
In your code this.length
is not the local variable length
in your function.
this
is just the global object window
, so this.length
is just window.length
.
(function() {
var people,length = 10;
for (people = 0; people < length; people++) {
setTimeout((function(people){
return function() {
console.log(people);
};
})(people), 1000);
}
})();
you mean like:
(function() {
var people,length = 10;
for (people = 0; people < length; people++) {
(function(index) {
setTimeout(function() { console.log(index); }, 1000);
})(people);
}
})();