Solved

JQuery: Spotty Ajax Performance

Posted on 2011-09-23
4
229 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

809 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