Skip to content Skip to sidebar Skip to footer

Javascript Execute Function After Other Function Completes

I have a function called save: function save(callback){ var ed=tinyMCE.get('body'); var data=ed.getContent(); $('#saving').show(); $('#save_err').hide(); $.post

Solution 1:

In context of the following method

$('body').on('click','#preview',function(){
    save(function(){
        window.open('/article/preview/<?phpecho$article_id; ?>','_blank');
    });
});

You need to call the callback method which you are passing as argument.

functionsave(callback){
    $.post(url,function(d){

        $('#saving').hide();
        if (d.suc == 1) {
            $('#saved').show();
            setTimeout(function () {
                $('#saved').hide();

                //CALL THE METHODcallback();  
            }, 2000);
        } else {
            $('#save_err').html(d.err).show();
        }
   },"JSON");
};

Solution 2:

You never invoke callback function, so it never redirects. You need to call it when timeout is over:

functionsave(callback) {
    var ed = tinyMCE.get('body');
    var data = ed.getContent();
    $('#saving').show();
    $('#save_err').hide();
    $.post('/article/ajax/save', {
        title: $('#title').val(),
        body: data,
        token: token,
        aid: <? php echo $article_id; ?>
    }, function (d) {
        $('#saving').hide();
        if (d.suc == 1) {
            $('#saved').show();
            setTimeout(function () {
                $('#saved').hide();
                callback();  // <---- call callback
            }, 2000);
        } else {
            $('#save_err').html(d.err).show();
        }
    }, "JSON");
};

Post a Comment for "Javascript Execute Function After Other Function Completes"