应用于Ajax + Jquery的SetInterval Javascript函数导致页面闪烁重新加载?

问题描述:

了以下功能将调用Ajax从MySQL数据库加载数据:应用于Ajax + Jquery的SetInterval Javascript函数导致页面闪烁重新加载?

function displayAll() {     
    clearInterval (stopCompoundingInt); //stop current Interval 
     sendAjax('search', 'q', function(responseText){ 
     $("#txtResp").html(responseText); 
      stopCompoundingInt = setInterval (function(){ // start a new interval with below conditions: 
       sendAjax('search', 'q', function(responseText){ 
       $("#txtResp").html(responseText); 
       }); 
     }, 5000); 
    }) 
} 

这里是一个地方,它被应用于:

var eInput = ""; 
stopCompoundingInt = 0; 
$('#searchbar').live('keyup',function() { 
    eInput = $(this).val(); 
    if (eInput.length > 0) { 

     clearInterval (stopCompoundingInt); 
     $('#display_all').hide(); 
     sendAjax('search', eInput, function(responseText){ 
      $("#txtResp").html(responseText); 
       stopCompoundingInt = setInterval (function(){ 
        sendAjax('search', eInput, function(responseText){ 
          $("#txtResp").html(responseText); 
        }); 
       }, 5000); 
     }) 
    } else { 
     displayAll(); // run the above function to show all events 
    } 
}); 

功能应该上运行如果文本框中没有ID =“搜索栏”的文本,则间隔为5秒

有没有人有任何建议可以改善此功能的性能?

非常感谢,

泰勒

你可能想尝试jQuery .load() functionjQuery Timers plugin。我相信重复使用他们的解决方案是相当不错的解决方案。

+0

好的谢谢。我其实只是决定改变功能执行的方式,并且效果很好。 – TaylorMac 2011-05-11 22:41:14