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.
- 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
1 Answer
Reset to default 7Assuming 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).