2016-01-21 57 views
0

我正在通过图表可视化数据。我想每隔X秒发送一次不同的ajax呼叫。例如,第一个Ajax调用将是first.php,在x秒之后,该调用将是second.php。我如何通过jQuery引入这个概念?定期发送不同的ajax呼叫

感谢提前的帮助。

回答

0

好吧,让我来举一个例子来您:

  • 让我们在first.php说你有:

    <?php echo 'First Response'; ?>

而且在second.php

`<?php echo 'Second Response'; ?>` 

输出服务器端脚本作为参数,以成功处理函数传递,所以你必须

success: function(data) { alert(data); // First Response }

success: function(data) { alert(data); // apple }

<script type="text/javascript"> function test(){ alert('return sent'); $.ajax({ type: "POST", url: "first.php", data: somedata; dataType:'text'; //or HTML, JSON, etc. success: function(response){ alert(response); } }); } </script>

1

最好把你的文件名在阵列中使用AJAX调用

["first.php", "second.php", ..] 

然后使用JavaScript setTimeout

`for(page in yourArray){ 
    (function fire() { 
    $.ajax({ 
    url: 'ajax/test.html', 
    success: function(data) { 
    //do your work with response 
    }, 
    complete: function() { 
    // Schedule the next request when the current one's complete 
    setTimeout(fire, yourXSeconds); 
    } 
    }); 
})(); 
}` 
+0

我upvoted你的答案,因为它是部分正确的做这样的事情。但是,如果我不知道预先调用的文件,我该怎么办? –