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?
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With