1

Hey guys am new to javascript actually..i am trying to find the download time of a file.So i have dind the size of the file and divided it with the current time ..but its not giving me the correct result.

The code i have tried

function get_filesize(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open("HEAD", url, true); // Notice "HEAD" instead of "GET",
                             //  to get only the header
xhr.onreadystatechange = function() {
    if (this.readyState == this.DONE) {
        callback(parseInt(xhr.getResponseHeader("Content-Length")));
    }
};
xhr.send();
}




get_filesize("http://upload.wikimedia.org/wikipedia/commons/9/96/Google_web_search.png", function(size) {

var estimatedtime = (new Date().getTime())/size;
console.log(estimatedtime);

});

when i done do this i get output as 32245538.389347337.

What am expecting is to get the time in hh:mm:ss in the console.

How can i acheive this ??..Any help would be great Thanks

lovejs
  • 35
  • 1
  • 4

1 Answers1

0

After you calculate the value, convert it to date like below (of course, change the hardcoded value to estimatedtime):

var time = new Date(32245538.389347337);
alert(time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds());
Joey Chong
  • 1,470
  • 15
  • 20
  • when I REplaced the image file with aq music file i got alert like nan nan nan – lovejs Dec 13 '14 at 08:25
  • i also get error like No 'content-length' header is present on the requested resource. Origin 'null' is therefore not allowed access. babe.html:21 NaN:NaN:NaN – lovejs Dec 13 '14 at 08:42
  • It is nothing with the code. You get nan:nan:nan because you can't initialize the date due to missing content-length. What is the response header from the server? If it is your own server, what server that you using. You need to check why the server did not return the content-length? – Joey Chong Dec 13 '14 at 12:58
  • i fixed the issue .but the time i am getting is completely wrong i get 57 minute estimate time for a single picture which is false – lovejs Dec 13 '14 at 14:45
  • My code above just show you how to convert from a number to date time as per question: how to get hh:mm:ss. Your estimated time `var estimatedtime = (new Date().getTime())/size;` is current time divided by size which is incorrent. Estimated time should be total size / transfer rate (bytes per seconds). – Joey Chong Dec 14 '14 at 02:29
  • ..can you tell me how can i get transfer rate with js ?? – lovejs Dec 14 '14 at 09:36
  • Hmmm.. This is a good question, you can't get the transfer rate if you did not perform a download. However, you can refer [Detect connection speed](http://ditio.net/2008/08/06/detect-connection-speed-with-javascript/), [algorithm to estimate download time](http://stackoverflow.com/questions/2779600/how-to-estimate-download-time-remaining-accurately). You can try google to find more. – Joey Chong Dec 14 '14 at 13:34
  • Chond thanx ..:) can you tell me how the timer can be moved i have tried settimeout but it isnt working – lovejs Dec 14 '14 at 17:22
  • You want the function to be execute once or at specific interval? So far what you had tried? I can't advise if you just said it is not working. Btw, you can refer [setTimeout](http://www.w3schools.com/jsref/met_win_settimeout.asp) – Joey Chong Dec 15 '14 at 01:26
  • please see http://stackoverflow.com/questions/27479824/make-the-clock-decrement to see what i have tried – lovejs Dec 15 '14 at 08:16