strange js onclick problem

Discussion in 'JavaScript' started by afridy, Feb 15, 2012.

  1. #1
    Hi folks

    i am calling two js functions in onclick event as follows

    onclick="add_emp();refresh();"
    Code (markup):
    now both above functions are ajax functions. first one passes an employee no to ad the new employee. 2nd reload the employee list php file.
    now problem is, the second refresh() function is not executing. if i just ad an alert('test') in to the refresh() ajax function code
    then it works. so whats the problem really.

    function refresh(){
    	
    	[B]alert('hai');[/B] //if i put this, then the second function is executed.
    	
    	var xmlhttp;
    	
    	if (window.XMLHttpRequest){
    	  // code for IE7+, Firefox, Chrome, Opera, Safari
    	  xmlhttp=new XMLHttpRequest();
    	}else{
    	  // code for IE6, IE5
    	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    Code (markup):
     
    afridy, Feb 15, 2012 IP
  2. Andre91

    Andre91 Peon

    Messages:
    197
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #2
    The "refresh()" function IS executing both times, whether you put the "alert('test')" or not, but the problem is your "refresh()" function is executing before your "add_emp()" can finish. JQuery runs asynchronously. So your "refresh()" function actually loads the same old content, not the updated stuff.

    When you add the "alert()" function in your "refresh()" function, its gives the "add_emp()" function time to complete, or rather it brings the "refresh()" function to a halt until you click "ok"/"close"/"whatever the button says".

    If you replace the "alert('hai');" function with something like
    
    doNothing(){}
    var t = setTimeout("doNothing()", 2000);
    
    Code (markup):
    It may work MOST times, if not all times, without a silly alert popup.


    But that's still not the right way of running an ajax callback. You gotta post your "add_emp()" function code for us to show you how to properly run the function after the ajax call.

    But try the above and tell me how that works.
     
    Andre91, Feb 16, 2012 IP
  3. afridy

    afridy Well-Known Member

    Messages:
    810
    Likes Received:
    116
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Hai Andre9,

    Thanks for your valuable reply. yes now i undestand the issue :)
     
    afridy, Feb 19, 2012 IP
  4. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #4
    why don't you execute the other function after the first function is executed?

    something like this..

    
    
    function func1(callback){
    
    ...
    
    if(callback) callback();
    
    }
    
    function func2(){
    
    ...
    
    }
    
    <button onclick="func1(func2)">Click </button>
    
    
    Code (markup):
     
    JohnnySchultz, Feb 20, 2012 IP
  5. afridy

    afridy Well-Known Member

    Messages:
    810
    Likes Received:
    116
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Thanks JohnnySchultz,

    Another very valuable reply, both methods given in this thread works for me folks!!!
     
    afridy, Mar 3, 2012 IP
  6. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #6
    You're welcome!
     
    JohnnySchultz, Mar 6, 2012 IP