I'm using the Uploadify plugin to allow users to upload files, and I have the progress bar working. Now I want to give the user an estimated time until pletion, but I'm unsure how to calculate it using Javascript.
Assume I have the following variables: uploadSpeed
(in kb/s), timeStarted
(a Javascript date object?), fileSize
(size of the file in bytes). How can I calculate a countdown until the file is fully uploaded?
I'm using the Uploadify plugin to allow users to upload files, and I have the progress bar working. Now I want to give the user an estimated time until pletion, but I'm unsure how to calculate it using Javascript.
Assume I have the following variables: uploadSpeed
(in kb/s), timeStarted
(a Javascript date object?), fileSize
(size of the file in bytes). How can I calculate a countdown until the file is fully uploaded?
3 Answers
Reset to default 7var uploadedSoFar = uploadSpeed * (Date.now() - timeStarted.milliseconds) / 1000;
var timeRemaining = ((fileSize - uploadedSoFar) / uploadSpeed) + ' seconds';
Is this just a mathematical question? If so, take the difference between the timeStarted and timeNow, multipy that with the uploadSpeed, take result and substract it from the fileSize and divide that by your uploadSpeed. That´s your remaining time (assuming uploadSpeed is constant at any time).
(fileSize - (timeNow - timeStarted) * uploadSpeed) / uploadSpeed
But a way more accurate way is to take the actual amount of bytes that have been uploaded yet to calculate the remaining time. That should be no problem since the user is uploading a file to your server. Therefore you can just read the partial file size from your server.
You don't really need the timeStarted variable since the uploadSpeed is not constant, you'd do better monitoring the ammount 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
}