[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

JQuery: Spotty Ajax Performance

Posted on 2011-09-23
4
Medium Priority
?
241 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

650 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