There is a function with a classic counter in closure:
function sliding(arr) { var i = 0; return function() { arr[i].style.opacity = 0; if(i == arr.length - 1) { arr[0].style.opacity = 1; } else { arr[i+1].style.opacity = 1; } i++; if(i == arr.length) { i = 0; } console.log(i); } } and code using this function:
function move(item, step) { for(var i = 0; i < item.length; i++) { (function(i, step) { var f = sliding(item[i]); //вот здесь использую замыкание setTimeout(f, step) // и вызываю новую функцию через таймер, по идее перемменная `i` сохраненная в замыкании должна увеличиваться...А она всегда равна единице })(i, step); step += 300; } } setInterval(move.bind(this, self.imgs, 2000), 4000); Why does not the variable in the closure increase?
var f = sliding(item[i]);every time you create a new closure , withvar i = 0;. So he prints one every time ... Can we consider the remote debugging session closed? - Dmitriy Simushev