Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.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 AJAX以设定的时间间隔在加载状态下运行_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript AJAX以设定的时间间隔在加载状态下运行

Javascript AJAX以设定的时间间隔在加载状态下运行,javascript,jquery,ajax,Javascript,Jquery,Ajax,当文档每20秒加载并运行一次时,我正在尝试让我的AJAX工作。我该怎么做呢?我已经让它每20秒工作一次,但无法让它在文档加载时工作。任何帮助都会很好 <script type="text/javascript" language="javascript"> window.setInterval(function(){ var jobid = $('#jobid').text(); var filename = $('#filename').text(); $.ajax(

当文档每20秒加载并运行一次时,我正在尝试让我的AJAX工作。我该怎么做呢?我已经让它每20秒工作一次,但无法让它在文档加载时工作。任何帮助都会很好

<script type="text/javascript" language="javascript">
window.setInterval(function(){

var jobid = $('#jobid').text();
var filename = $('#filename').text();


    $.ajax({
        url: "../progress_bar.php",
        type:'POST',
        data: {"value1": jobid, "value2": filename},
        dataType: 'json',
        success: function(responce){
                $("#progress").html(responce);
            } // End of success function of ajax form
        }); // End of ajax call 

    }, 20000);

</script> 

setInterval(函数(){
var jobid=$('#jobid').text();
var filename=$('#filename').text();
$.ajax({
url:“../progress\u bar.php”,
类型:'POST',
数据:{“value1”:作业ID,“value2”:文件名},
数据类型:“json”,
成功:功能(响应){
$(“#进度”).html(响应);
}//ajax表单的成功结束函数
});//ajax调用结束
}, 20000);

感谢

对于ajax请求,您应该始终使用递归超时而不是间隔(因为请求可能持续时间比间隔长,因此一次完成多个请求),这也解决了主要问题:

//may wrap the whole thing into an onload listener
(function update(){
  $.ajax({
    url: "../progress_bar.php",
    type:'POST',
    data: {"value1": jobid, "value2": filename},
    dataType: 'json',
    success: function(responce){
            $("#progress").html(responce);
            //update again in 20secs:
            setTimeout(update, 20000);
        } 
    });  

})(); //start immeadiately
一个小演示:

console.log(“加载”);
(函数next(){
控制台日志(“运行”);
设置超时(下一步,1000);

})()
对于ajax请求,应始终使用递归超时而不是间隔(因为请求可能持续时间比间隔长,因此一次完成多个请求),这也解决了主要问题:

//may wrap the whole thing into an onload listener
(function update(){
  $.ajax({
    url: "../progress_bar.php",
    type:'POST',
    data: {"value1": jobid, "value2": filename},
    dataType: 'json',
    success: function(responce){
            $("#progress").html(responce);
            //update again in 20secs:
            setTimeout(update, 20000);
        } 
    });  

})(); //start immeadiately
一个小演示:

console.log(“加载”);
(函数next(){
控制台日志(“运行”);
设置超时(下一步,1000);

})()
所以创建一个函数并调用它。。。。并将间隔设置为它…所以制作一个函数并调用它。。。。并设置它的间隔。。。。