2017-09-25 7 views
0

ドキュメントがロードされて20秒ごとに実行されるとき、私は現在AJAXを動作させようとしています。これをどうやってやるの?私は20秒ごとに動作するようにしましたが、ドキュメントが読み込まれたときに動作しません。どんな助けも素晴らしいだろう。AJAXは一定の間隔で実行されます。

<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> 

も解決さ1は常に再帰タイムアウトの代わりの間隔を(要求がより長い間隔を続くことが原因なので、一度行って複数の要求がある)を使用する必要がありますAJAXリクエストをありがとう

+0

関数を作成し、それを呼び出すと、間隔を設定します.... – epascarello

答えて

2

、主な問題:

//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("load"); 
 
(function next(){ 
 
    console.log("run"); 
 
    setTimeout(next, 1000); 
 
})()

関連する問題