0

how can I get the % of a image loading on my server and show % of it's loading. That's what I use to load image(it's for a simple download speed test).

var imageAddr = 'http://192.168.11.17/Cat2.JPG' + '?n=' + Math.random();
var startTime, endTime;
var downloadSize = 7616998;
var download = new Image();
download.onload = function () {
endTime = (new Date()).getTime();
showResults();
}
startTime = (new Date()).getTime();
download.src = imageAddr;
function showResults() {
  var duration = (endTime - startTime) / 1000; 
    var bitsLoaded = downloadSize * 8;
    var speedBps = (bitsLoaded / duration).toFixed(2);
    var speedKbps = (speedBps / 1024).toFixed(2);
    var speedMbps = (speedKbps / 1024).toFixed(2);
    var speedMB = speedMbps / 8;
    document.getElementById('download').innerHTML = speedMbps + 'Mbps';
}

Maybe there is other solutions with XMLHTTPRequest but I've tried with no succes. Thanks.

Zara Gheorghe
  • 488
  • 4
  • 13
  • Possible duplicate? http://stackoverflow.com/questions/14218607/javascript-loading-progress-of-an-image – RichieAHB Sep 29 '14 at 11:23
  • no it's not a duplicate because I don't want the image to be visible and in this case onLoadStart is not fired. – Zara Gheorghe Sep 29 '14 at 11:43
  • You don't have to append the image to the DOM - although they do in the example I linked to - so it need not be visible. In which case you can use `onLoadStart`. Would that not work? – RichieAHB Sep 29 '14 at 11:47

0 Answers0