Can I find an ETA to download an html 5 file?

I have a html5 script file upload on my site.

it works fine with this step progress script

xhr.upload.addEventListener("progress", function(e) { var pc = parseInt(100 - (e.loaded / e.total * 100)); var pci = parseInt(e.loaded / e.total * 100); //get us our ETA //kilobytes or megabytes? var pcia = e.loaded / 1024; var pcia2 = e.total / 1024; if (pcia2 > 1024) { pcia = pcia / 1024 pcia2 = pcia2 / 1024; progress.style.backgroundPosition = pc + "% 0"; elapsedtime = startTime + new Date(); alert(startTime);alert(new Date()); var eta = ((e.total / e.loaded) * elapsedtime) - elapsedtime; progress.innerHTML = pci + "% (" + Math.ceil(pcia * 100)/100 + " MB of " + Math.ceil(pcia2 * 100)/100 + " MB)-(" + eta +" secs. remaining)"; } else { progress.style.backgroundPosition = pc + "% 0"; progress.innerHTML = pci + "% (" + Math.ceil(pcia * 100)/100 + " KB of " + Math.ceil(pcia2 * 100)/100 + " KB)-(" + eta +" secs. remaining)"; } }, false); 

ok, so I warned about the start time and the time of the call. But the ETA variable shows as NaN. How can you achieve something like a date ("Ymd H: i: s"); (from php) in javascript?

+4
source share
1 answer

in order to find ETA, you must change the code to this.
the changes that are made here, we have two new variables called startTime and now.

put var startTime as soon as loading starts, and now it should be the same syntax as var.

this script will show users the full percentage, kilobytes / megabytes downloaded vs kb / mb, and now ETA to download the file.

 xhr.upload.addEventListener("progress", function(e) { var pc = parseInt(100 - (e.loaded / e.total * 100)); var pci = parseInt(e.loaded / e.total * 100); //get us our ETA //kilobytes or megabytes? var pcia = e.loaded / 1024; var pcia2 = e.total / 1024; if (pcia2 > 1024) { pcia = pcia / 1024 pcia2 = pcia2 / 1024; var now = (new Date()).getTime(); var elapsedtime = now - startTime; elapsedtime = elapsedtime / 1000; var eta = ((e.total / e.loaded) * elapsedtime) - elapsedtime; eta = Math.round(eta); progress.innerHTML = pci + "% (" + Math.ceil(pcia * 100)/100 + " MB of " + Math.ceil(pcia2 * 100)/100 + " MB)-(" + eta +" secs. remaining)";alert(pci); progress(pci, $('#progress')); } else { var now = (new Date()).getTime(); var elapsedtime = now - startTime; elapsedtime = elapsedtime / 1000; var eta = ((e.total / e.loaded) * elapsedtime) - elapsedtime; progress.innerHTML = pci + "% (" + Math.ceil(pcia * 100)/100 + " KB of " + Math.ceil(pcia2 * 100)/100 + " KB)-(" + eta +" secs. remaining)"; progress(pci, $('#progress')); } }, false); 
+3
source

Source: https://habr.com/ru/post/1497492/


All Articles