Reputation: 22859
I'm trying to run one function after another function completes.
$.when(saveCanvas(canvas)).done(setWallPaper());
Each function works fine on it's own but when I run the code above it only runs the first function.
What do I need to change?
Upvotes: 7
Views: 32917
Reputation: 22859
Looking back at this now, this seems to be how it works with jQuery:
function f1() {
alert("function one");
}
$.when( f1() ).done(function() {
alert("function 1 is done running function two");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Note: This is identical to my method I posted in the question so essentially it should have worked. Though jQuery may have changed over the last 3 years. Most likely was a problem with functions being called.
Upvotes: 3
Reputation: 461
another wild guess:
$.when(saveCanvas(canvas)).done(function(){
setWallPaper()
});
Upvotes: 2
Reputation: 4300
According to a comment on another SO question, $.when
expects deferred objects as arguments. If you don't pass anything, the callbacks will be invoked immediately.
Does setWallPaper()
appear to not be working because it is actually being run before saveCancas(canvas)
? saveCanvas()
is not actually a deferred object, which when
expects it to be. To make it a deferred object, add dfr = $.Deferred();
to the beginning of your saveCanvas()
function and return dfr.promise();
to the end of it. Check out this SO answer for more details.
function saveCanvas(canvas)
{
dfr = $.Deferred();
//Your code
return dfr.promise();
}
Read more: http://api.jquery.com/jQuery.when/
Upvotes: 9