1

Hey guys am trying to get an expected download time with an ajax script..It just works fine ..I get the expected time in the format hh:mm:ss.But the clock isnt moving The code i have tried

function get_filesize(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open("HEAD", url, true);
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;
    var time = new Date(estimatedtime);
    var c = setTimeout(function() {
        time.getHours() + ":" + time.getMinutes() + ":" +
            time.getSeconds()
    }, 500);

    console.log(c);

});

When i try this code i get an output like 1.

What i need as my output

Suppose the output i got is 00:57:12 i need to decrement the time as a timer like 00:57:11 ans so on.

I havnt no idea on how to try this ..i have tried setTimeout But it didnt helped me.

Any help would be greatly appreciated..Thanx

Community
  • 1
  • 1
lovejs
  • 35
  • 1
  • 4
  • time.getSeconds() - 1 ??? – Bhojendra Rauniyar Dec 15 '14 at 08:17
  • @Bhojendra-C-LinkNepal tried but didnt worked – lovejs Dec 15 '14 at 08:22
  • I'm a bit confused about how you calculate your estimated time? You get the current time and divide it by the file size? Doesn't make any sense to me. – Dimitar Dimitrov Dec 15 '14 at 09:11
  • Regardless the logic of how you get the estimated time, just want to point out that if you using setTimeout, it only will run once after you trigger the get_filesize function. If you want the timer to keep decreasing, you should use setInterval as per suggested by @ykc which it will run on every x milliseconds. Besides that, I found that you never update your time variable. The code in setTimeout will run but it will keep show the same value. – Joey Chong Dec 15 '14 at 09:11
  • @JoeyChong thanx man .can you tell me how can i get the transfer rate if an item is downloading ??..is transfer rate == bandwidth of my connection at download time ?? – lovejs Dec 15 '14 at 13:25
  • @JoeyChong i have searched all over but i couldnt find it ??..is the transfer rate equals to my bandwidth speed at time of download ? – lovejs Dec 15 '14 at 15:31
  • @JoeyChong ???????????????????????????? – lovejs Dec 15 '14 at 17:38
  • I thought I had give you some references on your previous post? Anyways, you also can refer to this [detect internet speed](http://stackoverflow.com/questions/5529718/how-to-detect-internet-speed-in-javascript) – Joey Chong Dec 16 '14 at 01:22

1 Answers1

0

You may try setInterval instead of setTimeout :

setInterval(function() {
   var c = time.getHours() + ":" + time.getMinutes() + ":" +
        time.getSeconds();
   console.log(c);
}, 500);

Edit : I updated the code above

ykc
  • 63
  • 1
  • 6
  • i get the same output which is 1 when i run this code – lovejs Dec 15 '14 at 11:20
  • can you tell me how can i decrease the hour,minute and seconds – lovejs Dec 15 '14 at 13:00
  • To decrease you should declare the c variable outside the scope of the function in the setInterval, and initialize it with the time you want in seconds, then in the setInterval, you set c = c - 1; (or c--; or c -= 1;) and you can display the result in your time format (see this link : http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-string-with-format-hhmmss) – ykc Dec 15 '14 at 13:11