?
Solved

JQuery: Spotty Ajax Performance

Posted on 2011-09-23
4
Medium Priority
?
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

764 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