Solved

JQuery: Spotty Ajax Performance

Posted on 2011-09-23
4
226 Views
Last Modified: 2012-05-12
Hello all, I have a script I am working on that works fine, but works intermittently. If I put an alert into my function, it is always triggered, but the ajax functions are not always fired. My form has 100 elements; could this be the issue? I have the same problem in IE9 and Firefox.

Performance is also the same whether I call the open_box function or not.

Thanks in advance for any help.


$(function(){
		$.ajaxSetup ({cache: false});
		$("table.locations-grid tr.data-row td").delegate(":button[name=transfer]","click",function(){	
			$("#transferForm :input[type=checkbox]").not(':checked').parent().prev().children("select").attr('disabled',true);
			$("#transferForm :input[type=checkbox]").not(':checked').attr('disabled',true);
			if ( $("table.locations-grid tr.data-row td :input[type=checkbox]:checked").length > 0 ){			
				$("table.locations-grid tr.data-row td :button[name=transfer]").attr("disabled", true);
				$(":input[type=checkbox][name=checkall]").prop("checked", false);
					$.post('ajax.php', $("#transferForm").serialize()+'&type=transfer' ,function(data){			
							$.each(data,function(key,value){
								$("#"+value['ItemId']+" td:nth-child(9)").html(value['Status']);
								$("#"+value['ItemId']+" td:nth-child(10) , #"+value['ItemId']+" td:nth-child(11)").html('');
							});
						open_box('http://127.0.0.1/design/ajax2.php',{'table': 'WhTransfers', 'type': 'showTransfer', 'id': data['0']['ParentId']} ,'910');			
				},"json");
			$("table.locations-grid tr.data-row td :button[name=transfer]").removeAttr("disabled");	
			$("#transferForm :input[type=checkbox]").parent().prev().children("select").removeAttr('disabled');
			$("#transferForm :input[type=checkbox]").removeAttr('disabled');		
			}
		});
		if ( $('body > table.locations-grid').size() > 1 ){
			$(":input[type=button]").remove(); 
			alert('Trying to load multiple grids - loss of database table scope.');
			return false;
		}else{	
			if ( $("table.locations-grid").attr('id') ){
				var table=$("table.locations-grid").attr('id');
			}
		}
		$(":input[type=checkbox][name=checkall]").click(function(){
			var nth=($(this).parent().index()+1);
			$(this).parent().parent().parent().children("tr").children("td:nth-child("+nth+")").children(":input[type=checkbox]").attr('checked',this.checked);
		});

	});

Open in new window

0
Comment
Question by:Candidochris
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36593548
Cannot see why the ajax would be spotty - do you make concurrent ajax calls that might interfere with each other?

In any case I suggest - especially sine you seem to have a large DOM - to cache the elements
$("table.locations-grid tr.data-row td").delegate(":button[name=transfer]","click",function(){  
      var chk = $("#transferForm :input[type=checkbox]").not(':checked');
      var sel = chk.parent().prev().children("select");
      var but = $("table.locations-grid tr.data-row td :button[name=transfer]"); 
      sel.attr('disabled',true);
      chk.attr('disabled',true);
      if ( $("table.locations-grid tr.data-row td :input[type=checkbox]:checked").length > 0 ){      
        but.attr("disabled", true);
        $(":input[type=checkbox][name=checkall]").prop("checked", false);
          $.post('ajax.php', $("#transferForm").serialize()+'&type=transfer' ,function(data){      
              $.each(data,function(key,value){
                $("#"+value['ItemId']+" td:nth-child(9)").html(value['Status']);
                $("#"+value['ItemId']+" td:nth-child(10) , #"+value['ItemId']+" td:nth-child(11)").html('');
              });
            open_box('http://127.0.0.1/design/ajax2.php',{'table': 'WhTransfers', 'type': 'showTransfer', 'id': data['0']['ParentId']} ,'910');      
        },"json");
      but.removeAttr("disabled");  
      sel.removeAttr('disabled');
      chk.removeAttr('disabled');    
      }
    });

Open in new window

0
 

Author Comment

by:Candidochris
ID: 36594087
Its a single call but it alters my database  so I don't think caching is an option because each call is unique.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 36594365
Caching the DOM elements. My changes did nothing to the ajax but traverses the dom less often than your code which ran over the table several times to get the checkbox the select and the button
0
 

Author Closing Comment

by:Candidochris
ID: 36600545
Ah, that makes great sense and solved my problem.

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript closure 1 35
Angular - break the dataset in chunks? 7 39
Add css and js to MVC Project not in project 9 27
Javascript and Jquery not firing 9 40
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now