尽管声明了上下文

时间:2015-09-05 15:18:24

标签: javascript scope

变量amountCompleted在类中设置,因为当它是全局变量时以及传递给函数时它变为未定义

调用递归函数auxReportProgress后,

amountCompleted变为未定义

如何使用以下setTimeout循环保持amountCompleted的上下文?

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();

如果需要更多信息,请与我们联系。任何帮助表示赞赏。

谢谢,

PV

0 个答案:

没有答案