The variable amountCompleted is set in a class because it became undefined when it was a global variable as well as when being passed into the function
amountCompleted becomes undefined after the recursive function auxReportProgress is called
How do I keep the context of amountCompleted with the following setTimeout loop?
function progressReport(importCount, progressID){
this.amountCompleted = 0;
this.importCount = importCount;
this.progressID = progressID;
}
progressReport.prototype.auxReportProgress = function() {
var self = this;
if (self.amountCompleted < this.importCount) {
var newAmount = reportProgress(this.progressID);
// make ajax call to get progress info
if (newAmount){
self.amountCompleted += newAmount;
// indicate new amount
}
setTimeout(function() {self.auxReportProgress();}, 3000);
}
}
function reportProgress(progressID){
jQuery.ajax({method:'post',url:"{{=URL(r=request,f='call', args=
['json','reportProgress'])}}",
data:{'progressID':progressID},
success: function(anyNewFiles){
var newCompleted =
anyNewFiles['newCompleted'];
return newCompleted.length;
}
});
}
var progReport = new progressReport(importCount, progressID);
progReport.auxReportProgress();
Let me know if more info is needed. Any help is appreciated.
Thanks,
PV