Skip to content

Instantly share code, notes, and snippets.

@artursapek
Created September 5, 2012 21:09
Show Gist options
  • Select an option

  • Save artursapek/3644800 to your computer and use it in GitHub Desktop.

Select an option

Save artursapek/3644800 to your computer and use it in GitHub Desktop.
preloading method
preload = (urls) ->
for url in urls
braces = url.match(/{\d\.\.\d}/g)
if braces
nums = braces[0].match(/\d/g)
expanded = []
for num in [nums[0]..nums[1]]
expanded.push url.replace(braces[0], num)
preload expanded
else
img = new Image()
img.onload = ->
console.log("Preloaded #{this.src}")
img.src = url
true
# Usage:
preload ['/static/tilda/loader.png', '/static/tilda/logo_{1..4}.png']
# Log:
# Preloaded http://localhost:8000/static/tilda/logo_1.png
# Preloaded http://localhost:8000/static/tilda/logo_2.png
# Preloaded http://localhost:8000/static/tilda/logo_3.png
# Preloaded http://localhost:8000/static/tilda/logo_4.png
# Preloaded http://localhost:8000/static/tilda/loader.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment