Last active
August 29, 2015 14:20
-
-
Save Duder-onomy/37e23c765452322bf03a to your computer and use it in GitHub Desktop.
Async Image Preloader Using Promises AMD
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
define(['jquery'], function($) { | |
'use strict'; | |
return function(urlArray) { | |
return $.when.apply($, urlArray.map(function(url) { | |
if(url) { | |
var $imgDeferred = new $.Deferred(); | |
setTimeout(function() { | |
var imageTag = new Image(); | |
imageTag.onload = $imgDeferred.resolve; | |
imageTag.onerror = $imgDeferred.reject; | |
imageTag.src = url; | |
}, 0); | |
return $imgDeferred.promise(); | |
} | |
})); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment