2014-01-16 52 views
0

在我的jQuery文件上传器我有以下可用变量:uploadedBytes,totalBytestimeStarted如何计算我上传的剩余时间?

如何使用这些变量计算上传剩余时间?我遇到了一个涉及uploadSpeed变量的类似问题。

我可以从这些变量推导出uploadSpeed吗?还是我能够仅使用这三个变量来计算剩余时间?

+0

呃..上传时间取决于速度,所以你需要这个。您无法从您拥有的变量中进行计算。 – putvande

+0

将取决于网络速度;如果你说“* x分钟基于2MB'线*” – MackieeE

+0

@putvande的消息,则可以使用平均上传速度给出近似值。 –

回答

2

假设uploadedBytes在上传过程中chaning。

- >当您知道上传开始时调用此脚本。

var timecontroller = setInterval(function(){ 
    timeElapsed = (new Date()) - timeStarted; //assumng that timeStarted is a Data Object 
    uploadSpeed = uploadedBytes/(timeElapsed/1000); //upload speed in second 
    callback((totalBytes - uploadedBytes)/uploadSpeed); //callback is the funciont that show the time to usder the only argoment is the second remaining to the end 
},1000) 

- >当文件被完全上传清晰间隔timecontroller

clearInterval(timecontroller) 

注意:timeStarted必须是一个数据对象。

告诉我,如果它的工作。 thx to @Stefano Sanfilippo - 我使用他的一些脚本。

+0

不得不重新排列它一点,但它作品。非常感谢! – timo

+1

你是摇滚伴侣! :)很高兴有帮助! - 也许你可以添加你的改变... :)如果有人找到了这个任务,需要答案。 – Frogmouth

+0

“注意:timeStarted必须是数据对象。”是它的Date对象吗? – werqious