let tab = ["a","b","c"];
var printI = function(){
console.log(tab[i]);
};
for (var i=0; i<tab.length;i++){
setInterval(printI, 1000*(i+1));
}
|
let tab = ["a","b","c"];
var i; // i sera égal à 3 lors des appels
var printI = function(){
console.log(tab[i]);
};
for (i=0; i<tab.length;i++){
setInterval(printI, 1000*(i+1));
}
|
for (var i=0; i<tab.length;i++){
setTimeout(function(){
console.log("in call back " + i);
}, 3000*(i+1));
}
Correction avec closure
"use strict";
var _loop = function _loop(i) {
setTimeout(function () {
console.log("in call back " + tab[i]);
}, 1000 * (i + 1));
};
for (var i = 0; i < tab.length; i++) {
_loop(i);
}
Correction :
for (let i=0; i<tab.length;i++){
setTimeout(function(){
console.log("in call back " + tab[i]);
}, 1000*(i+1));
}