Created
May 28, 2014 13:50
-
-
Save benbabics/58e684bc5cabc6bcf186 to your computer and use it in GitHub Desktop.
Image Preloader
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
preloadImage = (image_data) -> | |
deferred = $.Deferred() | |
# preload the image, so it's ready to display | |
cache_bust = ( new Date() ).getTime() | |
img = new Image() | |
img.src = "#{image_data.src}?size=large&cachebust=#{cache_bust}" | |
# image has loaded, resolve deferred | |
$( img ).on 'load', -> | |
deferred.resolve() | |
deferred.promise() | |
# load image | |
image_loader = preloadImage 'http://placehold.it/40x40' | |
image_loader.done -> console.log 'image has loaded successfully!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment