How can I wait for a click event to complete

前端 未结 4 506
梦毁少年i
梦毁少年i 2020-12-28 08:31

I add a click event handler to an element

 $(\".elem\").click(function(){
       $.post(\"page.php\".function(){
         //code1
      })
 })
4条回答
  •  悲&欢浪女
    2020-12-28 09:04

    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.

提交回复
热议问题