开发者

jQuery: ajaxSubmit as synchron call

I have a central JavaScript function:

function storeProperties() {
    $("form#systemProperties").ajaxSubmit({
        data: $("form#systemProperties").serialize(),
        type: 'post',
        url: '?url=super',
        success: function(response) {
            /* output & co */
       }
    });
}

This method is sometimes called on it's own, sometimes by a different method, i.e.:

function sendMessage() {
    storeProperties(); /* it'开发者_运维技巧s a precondition */

    /* let's do some magic */
}

The problem is that storeProperties() finishes asynchronously while ajaxSubmit is still processed. Unfortunately the magic shouldn't start before storeProperties() is done.

How can I either synchronize the storeProperties() call or trigger a second function as soon as it's done? Please note that there are more then one function which call storeProperties().

Many thanks!


Put your Let's do magic functionality in a seperate function and then pass it as a parameter to storeProperties. i.e.

    function storeProperties(successFunc)
{     
    $("form#systemProperties").ajaxSubmit
    (
        {         
            data: $("form#systemProperties").serialize(),         
            type: 'post',         
            url: '?url=super',         
            success: function(response)
            {             
                /* output & co */        
                if(successFunc && (typeof successFunc == 'function'))
                {
                    successFunc(response);
                }
                                  else
                                  {
                                   //do the default stuff
                                  }
            }     
        }
    ); 
} 

function sendMessage() 
{     
    storeProperties(function(data){doTheMagic(data);}); 
} 

function doTheMagic(data)
{
    /* let's do some magic */ 
}

Edit: Updated the storeProperties to receive a function as a parameter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜