When I perform an asynchronous request with jQuery Ajax, sometimes the response is returned quickly in 800 ms, somtimes it's slow and the response is returned in 2.50s (avg), and sometimes it hangs, and just shows the loading images. I'm not sure if it's because of my PHP code or jQuery Ajax code. I send some values using jQuery Ajax:
function get_detail_product(obj)
{
var id = obj.id ;
var $this = jQuery('#'+id);
var Thumb = jQuery('#Scroller div.Container') ;
jQuery.each(Thumb , function(){
jQuery(this).css('border' , '#ccc 2px solid');
});
$this.parent().css('border' , '#ff8500 2px solid') ;
var load_area = jQuery('.detail') ;
//ajax request
load_area.html("");
load_area.html('<div id="loading" style="margin-top:60px;margin-left:350px;"><img src="../images/loading.gif"><br>Loding ... </div>');
jQuery.ajax({
url: 'index.php?module=product&popup=on ',
type: 'POST',
data: 'pid=' + id ,
success: function(result) {
jQuery('#response').remove();
load_area.html(result);
jQuery('#loading').fadeOut(500, function() {
jQuery(this).remove();
});
}
});
}
and in the PHP file I have the following code to retrieve the requested data:
//ajax requests
if(isset($_POST['subcatid']) && is_numeric($_POST['subcatid']))
{
$subcatid = $_POST['subcatid'] ;
$products = $dbc->getAll("select * from xxproduct where xsubcatid='$subcatid'") ;
//send result
echo '<table cellpadding="0" cellspacing="0" border="0" id="ScrollerTable"><tr>';
foreach ($products as $p) : echo '<td><div style="border:#ccc 2px solid ; padding:0px;margin-top:20px ; margin-bottom:20px ; margin-left:8px ; margin-right:8px;" class="Container"><a href="javascript:void(0)" id="'.$p['xproductid'].'" onclick="get_detail_product(this)" ><img src="imgsize.phpw=100&h=100&img=../uploads/product/'.$p['xproductid'].'/'.$p['xproductid'].'__1.jpg" border="0"/></a><div style="background-color:#ccc ;text-align:center ; padding:5px; ">'.$p['xproductname'].'</div></div></td>';
endforeach ;
echo ' </tr></table>';
}
I wonder if you can find any mistakes in my code that cause the delay; additionally I'm using a PEAR DB object to interact with the database.
When i type the title of my question in this page, suggestions are returned in about 500 ms. Why is this ajax interaction so fast but mine is not?