I am logging a variable to the console as I move through a for loop, but I would like it to occur at intervals, so I am trying to use setTimeout
.
function foo(val) {
return function() {
setTimeout(function() {
console.log(val);
}, 2000);
};
};
$(function() {
for (var i = 1; i < 15; i++) {
$(foo(i));
};
});
The first instance works properly (it occurs after 2000 milliseconds), but the remaining iterations log the variable immediately. They don't have their own delays. What am I doing wrong?