Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery AJAX轮询JSON响应,基于AJAX结果或JSON内容进行处理_Javascript_Jquery_Ajax_Json_Polling - Fatal编程技术网

Javascript jQuery AJAX轮询JSON响应,基于AJAX结果或JSON内容进行处理

Javascript jQuery AJAX轮询JSON响应,基于AJAX结果或JSON内容进行处理,javascript,jquery,ajax,json,polling,Javascript,Jquery,Ajax,Json,Polling,我是中级JavaScript/jQuery程序员的新手,因此非常感谢具体的/可执行的示例 我的项目需要使用AJAX轮询一个URL,该URL返回包含要添加到DOM的内容的JSON,或者返回一条消息{“status”:“pending”},该消息指示后端仍在使用该内容生成JSON响应。其思想是,对URL的第一个请求触发后端开始构建JSON响应(然后缓存),随后的调用检查此JSON是否已准备就绪(在这种情况下,它已提供) 在我的脚本中,我需要以15秒到1:30分钟的间隔轮询此URL,并执行以下操作:

我是中级JavaScript/jQuery程序员的新手,因此非常感谢具体的/可执行的示例

我的项目需要使用AJAX轮询一个URL,该URL返回包含要添加到DOM的内容的JSON,或者返回一条消息{“status”:“pending”},该消息指示后端仍在使用该内容生成JSON响应。其思想是,对URL的第一个请求触发后端开始构建JSON响应(然后缓存),随后的调用检查此JSON是否已准备就绪(在这种情况下,它已提供)

在我的脚本中,我需要以15秒到1:30分钟的间隔轮询此URL,并执行以下操作:

  • 如果AJAX请求导致错误,请终止脚本
  • 如果AJAX请求成功,并且JSON内容包含{“status”:“pending”},则继续轮询
  • 如果AJAX请求成功,并且JSON内容包含可用内容(即除{“status”:“pending”}之外的任何有效响应),则显示该内容,停止轮询并终止脚本
我尝试过几种方法,但效果有限,但我感觉它们都比实际需要的更混乱。下面是一个框架函数,我已经成功地使用它一次发出一个AJAX请求,如果我从JSON响应中获得可用的内容,它就会完成它的工作:

// make the AJAX request
function ajax_request() {
  $.ajax({
    url: JSON_URL, // JSON_URL is a global variable
    dataType: 'json',
    error: function(xhr_data) {
      // terminate the script
    },
    success: function(xhr_data) {
      if (xhr_data.status == 'pending') {
        // continue polling
      } else {
        success(xhr_data);
      }
    },
    contentType: 'application/json'
  });
}
但是,此函数当前不执行任何操作,除非它接收到包含可用内容的有效JSON响应

我不知道该怎么做的话,只是评论。我怀疑另一个函数应该处理轮询,并根据需要调用ajax
request(),但我不知道ajax
request()将其结果传回轮询函数以便它能够正确响应的最优雅的方法


非常感谢您的帮助!如果我能提供更多信息,请告诉我。谢谢

您可以使用一个简单的超时来递归调用ajax\u请求

success: function(xhr_data) {
  console.log(xhr_data);
  if (xhr_data.status == 'pending') {
    setTimeout(function() { ajax_request(); }, 15000); // wait 15 seconds than call ajax request again
  } else {
    success(xhr_data);
  }
}
在那条线周围加上一个反检查,你就得到了最多的投票次数

if (xhr_data.status == 'pending') {
  if (cnt < 6) {
    cnt++;
    setTimeout(function() { ajax_request(); }, 15000); // wait 15 seconds than call ajax request again
  }
}
if(xhr\u data.status=='pending'){
if(cnt<6){
cnt++;
setTimeout(function(){ajax_request();},15000);//等待15秒,然后再次调用ajax请求
}
}

您不需要在错误函数中执行任何操作,除非您想发出警报或其他什么。一个简单的事实是,it错误将阻止调用success函数,并可能触发另一次轮询。

我想:

$(function ()
{
    // reference cache to speed up the process of querying for the status element
    var statusElement = $("#status");

    // this function will run each 1000 ms until stopped with clearInterval()
    var i = setInterval(function ()
    {
        $.ajax(
        {
            success: function (json)
            {
                // progress from 1-100
                statusElement.text(json.progress + "%");

                // when the worker process is done (reached 100%), stop execution
                if (json.progress == 100) i.clearInterval();
            },

            error: function ()
            {
                // on error, stop execution
                i.clearInterval();
            }
        });
    }, 1000);
});

非常感谢您的邀请。这是一个有点车,但这里是修复。roosteronacid的答案在达到100%后不会停止,因为clearInterval函数使用错误

以下是一个工作函数:

$(function ()
{
    var statusElement = $("#status");

    // this function will run each 1000 ms until stopped with clearInterval()
    var i = setInterval(function ()
    {
        $.ajax(
        {
            success: function (json)
            {
                // progress from 1-100
                statusElement.text(json.progress + "%");

                // when the worker process is done (reached 100%), stop execution
                if (json.progress == 100) clearInterval(i);
            },

            error: function ()
            {
                // on error, stop execution
                clearInterval(i);
            }
        });
    }, 1000);
});
clearInterval()函数将interval id作为参数,然后一切正常;-)

干杯
Nik

您可以使用javascript setInterval函数每5秒加载一次内容

var auto= $('#content'), refreshed_content; 
    refreshed_content = setInterval(function(){
    auto.fadeOut('slow').load("result.php).fadeIn("slow");}, 
    3000);
供参考-


谢谢您的回复!我发现Joel Potter的解决方案在我的情况下工作得更好-不过这很有用。setTimeout()是否会在延迟期间阻止脚本其余部分的执行?否。
setTimeout()
指定将来执行代码块的某个时间间隔。只有在超时过期且代码运行时,脚本的其余部分才会被阻止。“在短循环中,最好只执行setTimeout,给它一些喘息的空间。在某些情况下,您可能会使用setInterval()每50毫秒轮询一个函数,或者其他什么。在这种情况下,可能没有足够的时间在再次执行之前完成您正在尝试执行的操作。”