[英]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.