Skip to content

Instantly share code, notes, and snippets.

@ElemarJR
Created November 8, 2012 02:18
Show Gist options
  • Select an option

  • Save ElemarJR/4036146 to your computer and use it in GitHub Desktop.

Select an option

Save ElemarJR/4036146 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Deferred Objects</title>
</head>
<body>
<div id="output"></div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
var
log = function(msg) {
$('#output').text($('#output').text() + ' ' + msg);
},
printDone = function(obj) {
log(obj + ' done ..');
},
timer1 = $.Deferred( function (def) {
setTimeout(function() { def.resolve("timer 1"); }, 2000);
}).promise(),
timer2 = $.Deferred( function (def) {
setTimeout(function() { def.resolve("timer 2"); }, 4000);
}).promise();
timer1.done(function(data) { printDone(data)});
timer2.done(function(data) { printDone(data)});
$.when(timer1, timer2).done(function() { printDone('timer 1 and 2')})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment