javascript - 如何调用ajax函数直到已经运行ajax不给出响应?

标签 javascript php jquery ajax

我想调用我的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 再次调用。

enter image description here

最佳答案

使用 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/

相关文章:

javascript - 无法使用 IIS gzip javascript 文件

javascript - 如何使用 jquery 将更改保存到文本区域

javascript - 设置边框时div跳转

PHP - 以参数作为参数传递函数

php - 允许英文字符、中文、日文

javascript - 如何从链接中提取 href 属性并创建一个特定的模式?

jquery - 适用于第 n 个 child 但不适用于 div

javascript - 使用 Webpack 从 SASS 导入 CSS-url

php - WordPress 链接和 CSS

javascript - 使用 native 输入类型的 jQuery UI 的协议(protocol)相对 URL 方法 ="date"