我想调用我的ajax函数,直到已经运行ajax而不给出响应。
页面每秒加载一次,并且每秒 ajax 函数调用,但我想调用 ajax,直到已经运行的 ajax 函数没有响应。
我可以存储 ajax 函数刷新率,以便在此之后加载我的函数吗?
下面是我的代码:
function realtime_content()
{
var xmlhttp=false;
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp = new XMLHttpRequest();
}
if (xmlhttp)
{
RTupdate_query = "RTajax=1&DB=" + DB + "" + groupQS + usergroupQS + "&adastats=" + adastats + "&SIPmonitorLINK=" + SIPmonitorLINK + "&IAXmonitorLINK=" + IAXmonitorLINK + "&usergroup=" + usergroup + "";
xmlhttp.open('POST', 'demophp');
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
xmlhttp.send(RTupdate_query);
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("realtime_content").innerHTML = xmlhttp.responseText;
}
}
delete xmlhttp;
}
}
这是页面加载函数:
function refresh_display()
{
if ($start_count < 1)
{
realtime_content();
}
$start_count++;
if (ar_seconds > 0)
{
document.getElementById("refresh_countdown").innerHTML = "" + ar_seconds + "";
ar_seconds = (ar_seconds - 1);
setTimeout("refresh_display()",1000);
}
else
{
document.getElementById("refresh_countdown").innerHTML = "0"
realtime_content();
setTimeout("refresh_display()",1000);
}
}
我在此图像页面中添加了一个图像 AST_timeonVDADall_final.php 在页面加载时多次调用并进入挂起时间。
所以我想解决这个挂起时间,如果页面处于挂起状态,则没有请求转到此页面 AST_timeonVDADall_final.php。
待解决后,此页面 AST_timeonVDADall_final.php 再次调用。
最佳答案
使用 jQuery $.when()
。 Document Link
Description: Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.
多个ajax调用示例
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
// a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
// Each argument is an array with the following structure: [ data, statusText, jqXHR ]
var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"
if ( /Whip It/.test( data ) ) {
alert( "We got what we came for!" );
}
});
关于javascript - 如何调用ajax函数直到已经运行ajax不给出响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33384808/