I have a generic dictionary like so:
data holds a dictionary (key, value) pair:
for (var key in data) {
var interval = setInterval(function () {
getProgress(key, data[key], interval) }, 2500); // get percent progress every sec.
}
}
getProcess function that needs to execute for EACH data[key] item
function getProgress(machineId, operationId, interval) {
alert(machineId);
$.post("@Url.Action("Status","Packages")", {
machineId: machineId,
operationId: operationId
}, function (data) {
if (data) {
//blah blah...
} else {
clearInterval(interval); }
}
}
});
}
For some reason only the LAST machineId get’s setInterval. I need to have setInterval for EACH data item separately.
Classic for loop scoping problem. Each iteration of the for loop is executed in the same scope, so every function that is created inside the for loop access the same
keyvariable.Simple solution is to create a private scope for each iteration.