jquery - Javascript execute function after other function completes -


i have function called save:

function save(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()},2000);         }else{             $('#save_err').html(d.err).show();         }         return true;    },"json"); }; 

when element #preview clicked, save execute, , redirect happen after function completes.

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

i have used both accepted answer , highest rated answer here, neither working. redirect occurs before function completes. execute jquery function after function completes

what have tried:

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

what doing wrong?

in context of following method

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

you need call callback method passing argument.

function save(callback){     $.post(url,function(d){          $('#saving').hide();         if (d.suc == 1) {             $('#saved').show();             settimeout(function () {                 $('#saved').hide();                  //call method                 callback();               }, 2000);         } else {             $('#save_err').html(d.err).show();         }    },"json"); }; 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -