I add a click event handler to an element
$(\".elem\").click(function(){
$.post(\"page.php\".function(){
//code1
})
})
Javascript execution is line by line. So whatever comes up, will be executed first. So adding the click code before the other method will work.
Plus if there is any async call, then take a flag which is set only when you get response.
var clicked = false; $('#elem').click(function(){ // do some async process clicked = true; });
while (!clicked){ // do nothing }
// other function to be called
Or the second option will be, if using post method, set async = true in property.