Last active
August 29, 2015 13:58
-
-
Save judy2k/9950763 to your computer and use it in GitHub Desktop.
Preload an image before calling a callback function
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 preloadImage = function(url, callback) { | |
var loaded = false, | |
loadHandler = function() { | |
if (!loaded) { | |
callback(url); | |
} | |
loaded = true; | |
}, | |
img = $('<img>') | |
.one('load', loadHandler) | |
.attr('src', url); | |
if ($(img).first().complete) { | |
loadHandler(url); | |
} | |
} |
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 preloadImage = function(url, callback) { | |
var loaded = false, | |
loadHandler = function() { | |
if (!loaded) { | |
callback(url); | |
} | |
loaded = true; | |
}, | |
img = document.createElement('img'); | |
img.addEventListener('load', loadHandler); | |
img.src = url; | |
if (img.complete) { | |
loadHandler(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment