lovejs lovejs - 4 months ago 18
Ajax Question

Calculating download time with javascript

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

Answer

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());