Skip to content

Instantly share code, notes, and snippets.

@kazu69
Last active December 11, 2015 21:28
Show Gist options
  • Save kazu69/4662610 to your computer and use it in GitHub Desktop.
Save kazu69/4662610 to your computer and use it in GitHub Desktop.
jQuery image preloader
images = [
image1.jpg,
image2.jpg,
image3.jpg,
image4.jpg
]
preload: (images, callback) ->
count = images.length
# no image
if count == 0
callback()
return
loaded = 0
error = false
load = (url) ->
$('<img>').attr('src', url).load( (response) ->
loaded++ if $(@).get(0)
callback() if count == loaded
).error( ->
# somethig error events
)
load url for url in images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment