2
$('.myDiv').click(function(event){ $.ajax({ url: '', dataType: 'json', success: function(json) { for (var i = 0; i < json.length; i++) { var response = json[i]; $('.result_new').append('<p>' + response.name + '</p>'); } //$('.content').append('<p>' + response.total + '</p>'); } }); 

})

event.stopPropagation()

isn't preventing the ajax call from being called repeatedly. Is there a function for this in Jquery?

1
  • I don't see anything calling this repeatedly. Are you saying the click event is being triggered repeatedly? Commented Sep 27, 2011 at 2:27

2 Answers 2

2
$('.myDiv').click(function(){ if( !$(this).hasClass('loading') ) { //add control class $(this).addClass('loading'); //do ajax... $.ajax({ success: function() { $('.myDiv').removeClass('loading') } }) } }); 
Sign up to request clarification or add additional context in comments.

Comments

1

event.stopPropagation() only prevents the event from bubbling up (from .myDiv to it's parent element until reaching the window). It doesn't prevent the function from executing.

You can use various methods to identify whether the request was sent or not, for example set .data(), e.g:

$(".myDiv").click(function() { if (typeof $(this).data('inTheMiddleOfAnAJAXCall') == "undefined") { $(this).data('inTheMiddleOfAnAJAXCall', true); // Create an AJAX Call } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.