Reputation: 427
I want a php program to be executed every 5 seconds using JavaScript. How can I do that?
I tried using:
<script type="text/javascript">
setInterval(
function (){
$.load('update.php');
},
5000
);
</script>
But it doesn't work.
Upvotes: 2
Views: 9323
Reputation: 17651
Using jQuery and setInterval
:
setInterval(function() {
$.get('your/file.php', function(data) {
//do something with the data
alert('Load was performed.');
});
}, 5000);
Or without 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);
Upvotes: 17
Reputation: 2007
Try using setInterval()
to perform a XHR call. (jQuery, non jQuery)
setInterval(function() {
// Ajax call...
}, 5000);
This will execute your code inside the function every 5 secs
Upvotes: 7