Created
January 7, 2014 10:47
-
-
Save xtrasmal/8297702 to your computer and use it in GitHub Desktop.
Calculate speed using javascript http://stackoverflow.com/questions/4583395/calculate-speed-using-javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var imageAddr = "yourimage.jpg" + "?n=" + Math.random(); | |
var startTime, endTime; | |
var downloadSize = [size here...]; | |
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); | |
alert("Your connection speed is: \n" + | |
speedBps + " bps\n" + | |
speedKbps + " kbps\n" + | |
speedMbps + " Mbps\n" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment