最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I calculate the time remaining for my upload? - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited Mar 6, 2018 at 21:16 drenda 6,24412 gold badges76 silver badges154 bronze badges asked Jan 16, 2014 at 13:09 timotimo 2,1691 gold badge28 silver badges41 bronze badges 7
  • 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
 |  Show 2 more ments

1 Answer 1

Reset to default 14

Assuming 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.

发布评论

评论列表(0)

  1. 暂无评论