3

I have been working on a file upload script using uploadify that calculates the remaining time of the upload. Uploadify offers a callback called onUploadProgress, which gives me some information on the current upload.

I have this working to an extent, but the issue is on safari. The time remaining script is not accurate, it will jump around between say, 10 and 20 minutes remaining on a large file - this is acceptable. On Safari, the deviation is huge, it will move between 0 and 300 minutes remaining, even on small uploads.

The idea I had was to average the time remaining by adding up the timeLeft value on each progress (total), and incrementing a value by 1 (numberofpolls), so the average could be calculated by total/numberofpolls.

The code below contains the function for CalcTime and the callback for onUploadProgress.

var total = 0;
var numberofpolls = 0;
var avg = 0;    

function CalcTime(newTime)
{




    total += parseFloat(newTime);

    numberofpolls++;

    avg = (total / numberofpolls);

    return avg;
}

...



'onUploadProgress' : function(file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {

     // remaining amount of bytes in upload            
     var remaining = totalBytesTotal - totalBytesUploaded;

     // approx amount of time left.         
     var timeLeft = (((remaining - (Date.now() - timeStarted) * this.queueData.averageSpeed) / this.queueData.averageSpeed) /60)/1000;

     var tmp_time = CalcTime(timeLeft );


     if (tmp_time<=1)
        var suffix = "Less than a minute remaining";
      else
        var suffix = ~~(tmp_time)+ ' minute(s) remaining';
}

The problem isn't in the calculation of timeLeft - as this worked before, the issue is averaging the values of timeLeft via CalcTime. If there is a better way to stop safari doing this, or a better way to calculate it, any help would be appreciated.

Thanks in advance.

verenion
  • 383
  • 1
  • 13
  • 1
    Try using an exponential moving average. See [How to estimate download time remaining (accurately)?](http://stackoverflow.com/questions/2779600/how-to-estimate-download-time-remaining-accurately) – quietmint Jul 06 '13 at 15:03

0 Answers0