间隔上的Ajax [英] Ajax on interval
问题描述
我在滞后和脚本执行方面遇到麻烦.
I'm having trouble with lag and script execution.
我有以下代码:
$(function(){
$("#results").html("Loading work order queue...");
setInterval("showWorkOrders();", 15000)
});
function showWorkOrders()
{
$.ajax({
url: '/ajax/job_queue_ajax.php',
type: 'POST',
cache: false,
data: 'update=true',
success: function(data)
{
$("#results").html(data);
$('span#ref_msg').html('');
},
error: function()
{
a = new customModal('ajax');
$("#results").html("<font class='msgDivError'>"+a.ajaxError+"</font>");
$('span#ref_msg').html('');
}
});
}
我的后端PHP(仅作为示例):
My backend PHP (only an example):
$SQL = MySQL_query("SELECT * FROM table")
while($data = MySQL_fetch_array($SQL))
{
//// OUTPUTS A TABLE WITH INFORMATION
}
现在,整个过程执行起来非常滞后(加载时间很长),有时会由于加载时间太长而导致脚本错误.如果延长刷新间隔,情况会开始好转(滞后仍然存在),并且不会出现执行错误.我需要间隔很短的时间,但是我无法找出有效的方法.关于如何改善这一点的任何建议?
Now, this whole thing executes however with a lot of lag (takes very long to load) and sometimes creates an script error because it took too long to load. If I extend the refresh interval, things start getting better (the lag remains) and I don't get the execution error. I need to have a short interval for what I need, but I cannot figure out an efficient way of doing so. Any suggestions on how to improve this?
此外,在进入具有刷新功能的页面一段时间后,浏览器变得非常缓慢,直到锁定为止...
Also, after a while of being on the page that has the refresher, the browser becomes extremely slow to the point where it locks...
推荐答案
我会改用setTimeout
来解决它:
$(function(){
$("#results").html("Loading work order queue...");
showWorkOrders();
});
function showWorkOrders()
{
$.ajax({
url: '/ajax/job_queue_ajax.php',
type: 'POST',
cache: false,
data: 'update=true',
success: function(data)
{
$("#results").html(data);
$('span#ref_msg').html('');
setTimeout("showWorkOrders();", 5000)
},
error: function()
{
a = new customModal('ajax');
$("#results").html("<font class='msgDivError'>"+a.ajaxError+"</font>");
$('span#ref_msg').html('');
setTimeout("showWorkOrders();", 5000)
}
});
}
这意味着它要等待5秒钟才能执行新的Ajax请求,而前一个请求可能要花多长时间.仍然要等待5秒钟,然后才能再次尝试.
This would mean that it waits 5s before doing a new ajax request, the previous one can take however long it wants. Still waits 5s before it tries again.
这篇关于间隔上的Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!