Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery .When .done

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?

like image 550
Philip Kirkbride Avatar asked Mar 24 '13 02:03

Philip Kirkbride


1 Answers

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/

like image 58
Luke Shaheen Avatar answered Oct 03 '22 03:10

Luke Shaheen