Created
February 22, 2013 17:41
-
-
Save jshaw/5015215 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html class="no-js" lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Deferred</title> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<script> | |
var images = [ | |
"http://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Hausziege_04.jpg/600px-Hausziege_04.jpg", | |
"http://www.whitmorefarm.com/images/gallery/original/jason.jpg", | |
"http://almostdorothy.files.wordpress.com/2010/08/goat3a.jpg", | |
"http://www.angaelica.com/wp-content/uploads/2010/10/Goats1-300x200.jpg" | |
]; | |
var load = function (images) { | |
var index = images.length; | |
var promises = []; | |
while (index--) { | |
promises.push(defer(images[index])); | |
} | |
return promises; | |
}; | |
var defer = function (src) { | |
var deferred = $.Deferred(); | |
var image = new Image(); | |
image.onload = function () { | |
deferred.resolve(); | |
} | |
image.src = src; | |
return deferred.promise(); | |
}; | |
$.when | |
.apply(null, load(images)) | |
.done(function() { | |
console.log('images done'); | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment