2017-04-01 67 views
-1

我是js和ajax的新手。 我想要一個代碼調用disp.php文件並在load.php中顯示其輸出。 ajax代碼應該將值從load.php傳遞到disp.php,每次通話之間必須有5秒的延遲。使用ajax的多個HTTP請求

基本上我想替換下面的PHP代碼,這將由於腳本超時而失敗。 load()disp()disp.php

require("disp.php"); 

for($i = 1; $i <= $pt[1]; $i++) 
{ 
    load($mat,$i); 
    disp(); 
    sleep(5);  
} 
+1

哪裏是阿賈克斯進入功能圖片在這裏?這完全是PHP。 – harryjohn

+0

我想用ajax替換上面的'for'循環,即逐個調用php腳本以防止超時 –

+0

Stackoverflow不是免費的代碼編寫服務或*「how to」*教程服務 – charlietfl

回答

1

爲了從Javascript調用Ajax看到這個How to make an AJAX call without jQuery?

並撥打電話後,每隔五秒鐘使用的setTimeout

<script> 
    function callLoad() { 
     ......write logic to call the PHP file here... 
    } 

    var callCount = 0; 

    function callLoop() { 
     callLoad(); 
     callCount ++; 

     if(callCount == 5) return; // Stop the loop 

     setTimeout(function() { 
      callLoop(); 
     }, 5000); // Wait for 5 second to make the call again 

    } 

    window.onload = callLoop; // Start the loop after the page has finished loading 
</script>