#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    21
    Rep Power
    0

    jQuery AJAX running multiple times


    Hi have a DIV which I'm running a script on when a user clicks on it. The script does what it should however it's running several times, 5 is the most.

    Why?

    I get the console.log rows multiple times and the alert multiple times.

    Code:
    	$(document).on("click","#schedule",function(e){
    		RunScript();			
    	})
    
    	function RunScript(){
                   alert('d');
    		if (Dep != '' && Arr != ''){	
    		console.log("Started");		
    			$.ajax({			
    				method: "POST",
    				url: "scripts/insert.php",
    				dataType: "json",
    				data: { ..... }
    			})
    			.done(function( result ) {
    				console.log(result);
    			})
    			.fail(function(){
    				  alert('OOPS');
    				  console.log(arguments);
    			});
    		}
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Location
    Somerville, MA
    Posts
    51
    Rep Power
    72
    You might want to try
    Code:
            $(document).unbind().on("click","#schedule",function(e){
                RunScript();			
    	})
    The example is difficult to debug without what the vars Dep and Arr are. Does the ajax call run more than once if you remove the check for those values?
    Web Developer/Designer here

IMN logo majestic logo threadwatch logo seochat tools logo