Last active
August 29, 2015 14:04
-
-
Save mikaelkaron/215a54f962cb57243f09 to your computer and use it in GitHub Desktop.
Simple speed tester
This file contains 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
(function (sources, delay, success, fail) { | |
function onload() { | |
this.stop = new Date().getTime(); | |
window.setTimeout(next, success.apply(this, arguments) || delay); | |
} | |
function onerror() { | |
this.stop = new Date().getTime(); | |
window.setTimeout(next, fail.apply(this, arguments) || delay); | |
} | |
function next() { | |
var source = sources.pop(); | |
var img; | |
if (source !== void(0)) { | |
img = new Image(); | |
img.onload = onload; | |
img.onerror = onerror; | |
img.start = new Date().getTime(); | |
img.src = source + "?" + img.start; | |
} | |
} | |
next(); | |
})([ | |
"http://example.com/img-1.jpg", | |
"http://example.com/img-2.jpg", | |
"http://example.com/img-3.jpg", | |
"http://example.com/img-4.jpg", | |
"http://example.com/img-5.jpg", | |
], 100, function () { | |
console.info("send", "timing", "SpeedTest", "sampleLoadTime", this.stop - this.start , this.src); | |
}, function () { | |
console.error("send", "timing", "SpeedTest", "sampleErrorTime", this.stop - this.start , this.src); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment