Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Created September 23, 2015 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/54122a96d9291c558883 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/54122a96d9291c558883 to your computer and use it in GitHub Desktop.
Test if an image does NOT load
// From: http://jsfiddle.net/jfriend00/qKtra/ & http://stackoverflow.com/questions/9714525/javascript-image-url-verify/9714891#9714891
function testImage(url, callback, timeout) {
timeout = timeout || 5000;
var timedOut = false, timer;
var img = new Image();
img.onerror = img.onabort = function() {
if (!timedOut) {
clearTimeout(timer);
callback(url, "error");
}
};
img.onload = function() {
if (!timedOut) {
clearTimeout(timer);
callback(url, "success");
}
};
img.src = url;
timer = setTimeout(function() {
timedOut = true;
callback(url, "timeout");
}, timeout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment