tell javascript to run jquery ajax first -


in short, how let alert(1) run first:

    $.post('example.php', function() {         alert(1);     })     alert(2);     alert(3);     alert(4); 

but jquery ajax call seem run in asynchronous method. javascript run below first, alert(2) alert(4), post method, alert(1).

certainly can put code in ajax function, make no sense when have dozens of functions, have add code functions.

    $.post('example.php', function() {         alert(1);         example();     })      function example() {         alert(2);         alert(3);         alert(4);     } 

i want json data ajax call, , use later. there smart solution?

in jquery prefer use $.when , $.then it's easy , code more readable using this.

function catchthefish(){ console.log('we catching fish'); } function eatthefish(){ console.log('now time eat fish'); } $.when ( catchthefish() ).then( eatthefish() ); 

this code work in latest version of jquery 1.9.1


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -