在我的 jQuery 文件 uploader 中,我有以下可用变量:uploadedBytes
、totalBytes
和 timeStarted
。
如何使用这些变量计算上传的剩余时间?我遇到了一个类似的问题,涉及 uploadSpeed
变量。
我可以从这些变量中推导出uploadSpeed
吗?或者我可以仅使用这三个变量来计算剩余时间吗?
最佳答案
假设 uploadedBytes
在上传过程中发生变化。
-> 当您知道上传开始时调用此脚本:
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)
-> 文件完全上传后,清除间隔timecontroller
:
clearInterval(timecontroller)
注意timeStarted
必须是Date对象。
告诉我它是否有效。 感谢@Stefano Sanfilippo - 我使用了他的一些脚本。
关于javascript - 如何计算上传的剩余时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21162749/