Created
September 5, 2012 21:09
-
-
Save artursapek/3644800 to your computer and use it in GitHub Desktop.
preloading method
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
| 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