Solved

JQuery: Spotty Ajax Performance

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

831 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