Calculating the remaining download time

I am using the Uploadify plugin to allow users to upload files and the progress bar is working for me. Now I want to give the user an approximate time to complete, but I'm not sure how to calculate it using Javascript.

Suppose I have the following variables: uploadSpeed

(in kb / s), timeStarted

(Javascript date object?), fileSize

(File size in bytes). How can I calculate the countdown until the file is fully downloaded?


source to share

3 answers

var uploadedSoFar = uploadSpeed * ( - timeStarted.milliseconds) / 1000;
var timeRemaining = ((fileSize - uploadedSoFar) / uploadSpeed) + ' seconds';




Is this just a math question? If so, take the difference between timeStarted and timeNow, remember with uploadSpeed, take the result and subtract it from the file and split it using uploadSpeed. This is your remaining time (assuming uploadSpeed ​​is constant at all times).

(fileSize - (timeNow - timeStarted) * uploadSpeed) / uploadSpeed


But a more accurate way is to take the actual number of bytes that have been downloaded yet to calculate the remaining time. This shouldn't be a problem since the user is uploading the file to your server. So you can just read the size of the partial file from your server.



You don't need the timeStarted variable since uploadSpeed ​​is not constant, you are better off keeping track of the number of bytes uploaded.

var uploaded  // ammount of bytes uploaded
setInterval("updateProgress()", 1000) //every second updates the uploaded counter
function updateProgress(){
    uploaded += uploadSpeed //increments the ammount of bytes uploaded in a second
    updateProgressBar((uploaded/fileSize)*100) // update progress bar




All Articles