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

javascript - Calculate remaining download time - Stack Overflow

programmeradmin1浏览0评论

I am uploading files via Ajax in chunks. At the moment, each chunk weighs 50 KiB.

In the upload process I have the following information:

  • File size
  • Amount of chunks
  • Time started
  • How long it took to upload current chunk in ms

I can also add pretty much anything that could be needed to plete this, what I have thought of is not to rely on upload speed, but to rely on average chunk upload time, this is my current broken formula:

(averageUplTime * ((FileSize / ChunkSize) ) -  AmountOfChunks) / 1000 

It actually almost works, I can see between numbers that that it's decreasing in almost correct way, but i get these long numbers 9.16174 and I can't figure out right way to do this.

I am uploading files via Ajax in chunks. At the moment, each chunk weighs 50 KiB.

In the upload process I have the following information:

  • File size
  • Amount of chunks
  • Time started
  • How long it took to upload current chunk in ms

I can also add pretty much anything that could be needed to plete this, what I have thought of is not to rely on upload speed, but to rely on average chunk upload time, this is my current broken formula:

(averageUplTime * ((FileSize / ChunkSize) ) -  AmountOfChunks) / 1000 

It actually almost works, I can see between numbers that that it's decreasing in almost correct way, but i get these long numbers 9.16174 and I can't figure out right way to do this.

Share Improve this question edited Nov 24, 2012 at 14:35 phant0m 16.9k6 gold badges51 silver badges84 bronze badges asked Nov 24, 2012 at 13:33 LinasLinas 4,40819 gold badges74 silver badges120 bronze badges 2
  • 2 What's the question? Do you just want the rounded number? – MikeSmithDev Commented Nov 24, 2012 at 13:37
  • @MikeSmithDev No the formula is incorrect, i was hoping some one could figure out a better one – Linas Commented Nov 24, 2012 at 13:39
Add a ment  | 

1 Answer 1

Reset to default 7

Assuming startTime is a timestamp in milliseconds since the epoch, this should work:

var elapsedTime = (new Date().getTime()) - startTime;
var chunksPerTime = currentChunk / elapsedTime;
var estimatedTotalTime = amountOfChunks / chunksPerTime;
var timeLeftInSeconds = (estimatedTotalTime - elapsedTime) / 1000;

var withOneDecimalPlace = Math.round(timeLeftInSeconds * 10) / 10;

This is only "accurate" as long as the upload speed doesn't fluctuate very much. You can get better results by only considering the last X chunks to calculate chunksPerTime (and averaging about the last Y values of these).

发布评论

评论列表(0)

  1. 暂无评论