In my jQuery file uploader I have the following available variables: uploadedBytes
, totalBytes
and timeStarted
.
How do I caculate the time remaining for an upload using these variables? I came across a similar question that involved an uploadSpeed
variable.
Can I deduce the uploadSpeed
from these variables? Or am I able to calculate time remaining using only those three variables?
In my jQuery file uploader I have the following available variables: uploadedBytes
, totalBytes
and timeStarted
.
How do I caculate the time remaining for an upload using these variables? I came across a similar question that involved an uploadSpeed
variable.
Can I deduce the uploadSpeed
from these variables? Or am I able to calculate time remaining using only those three variables?
- Well.. the time it takes to upload depends on the speed, so you need that. You can not calculate it from the variables you have. – putvande Commented Jan 16, 2014 at 13:11
-
Would depend on one's network speed; if you said a message saying "x mins based on a
2MB
line" – MackieeE Commented Jan 16, 2014 at 13:11 - @putvande you can give an approximation using the average upload speed . – Stefano Sanfilippo Commented Jan 16, 2014 at 13:13
-
the
uploadedBytes
changes during the upload? – Frogmouth Commented Jan 16, 2014 at 13:17 -
@Frogmouth Yes,
uploadedBytes
is called upon during upload progress and updated accordingly. – timo Commented Jan 16, 2014 at 13:21
1 Answer
Reset to default 14Assuming that uploadedBytes
is changing during upload.
-> Call this script when you know that the upload starts:
var timecontroller = setInterval(function(){
timeElapsed = (new Date()) - timeStarted; // Assuming that timeStarted is a Date Object
uploadSpeed = uploadedBytes / (timeElapsed/1000); // Upload speed in second
// `callback` is the function that shows the time to user.
// The only argument is the number of remaining seconds.
callback((totalBytes - uploadedBytes) / uploadSpeed);
}, 1000)
-> When the file was fully uploaded, clear interval timecontroller
:
clearInterval(timecontroller)
Pay attention that timeStarted
must be a Date object.
Tell me if its work. Thanks to @Stefano Sanfilippo - I use some of his script.