Topic: Estimated time of upload completion
Hi all!
I have successfully written a function to estimate when the upload will be ready,in minutes!
First declare these variables and set keepTrack=1
var Total_file_size_to_upload=0;
var startSeconds,currentSeconds,diffSeconds,ETA,keepTrack;
keepTrack=1;
ETA=0;
Then lets collect the total size of upload files(multiple files or single files):
FilesAdded: function(up, files) {
plupload.each(files, function(file){
Total_file_size_to_upload +=file.size;
});
}
If the user removed a file, update the total upload size:
FilesRemoved: function(up, files) {
plupload.each(files, function(file){
Total_file_size_to_upload -=file.size;
});
},
Now,when upload is started, in the UploadProgress event, start a timer only once,for calculating the ETA of upload. "console.log(rectime(ETA))" will print the ETA of upload like this '02:32'
UploadProgress: function(up, file) {
if(keepTrack==1){
startSeconds = new Date();
setInterval(function() {
currentSeconds = new Date();
diffSeconds =currentSeconds.getTime() - startSeconds.getTime();
ETA=parseInt(bytesConvert(Total_file_size_to_upload,'kb')/bytesConvert(up.total.bytesPerSec,'kb')-parseInt(diffSeconds/1000, 10), 10);
}, 1000);
keepTrack=0;
}
console.log(rectime(ETA));
},
Finally add these functions to convert seconds in the minutes:seconds format and convert bytes in kb:
function rectime(sec) {
var hr = Math.floor(sec / 3600);
var min = Math.floor((sec - (hr * 3600))/60);
sec -= ((hr * 3600) + (min * 60));
sec += ''; min += '';
while (min.length < 2) {min = '0' + min;}
while (sec.length < 2) {sec = '0' + sec;}
hr = (hr)?':'+hr:'';
return hr + min + ':' + sec;
}
function bytesConvert(bytes,to){
if(to=="kb"){
return Math.round((bytes/1024)*1)/1;
}else if(to=="mb"){
return Math.round((bytes/1024/1024)*1)/1;
}else if(to=="gb"){
return Math.round((bytes/1024/1024/1024)*1)/1;
}
}
I think the developer should include a function like this, built-in the library!
Enjoy!