[英]How can I load a PHP file every 5 seconds with JavaScript
我希望使用JavaScript每5秒執行一次php程序。 我怎樣才能做到這一點?
我試過用:
<script type="text/javascript">
setInterval(
function (){
$.load('update.php');
},
5000
);
</script>
但它不起作用。
使用jQuery和setInterval
:
setInterval(function() {
$.get('your/file.php', function(data) {
//do something with the data
alert('Load was performed.');
});
}, 5000);
或者沒有jQuery:
setInterval(function() {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
console.log(request.responseText);
}
}
request.open('GET', 'http://www.blahblah.com/yourfile.php', true);
request.send();
}, 5000);
嘗試使用setInterval()
執行XHR調用。 ( jQuery , 非jQuery )
setInterval(function() {
// Ajax call...
}, 5000);
這將每5秒在函數內執行一次代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.