Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery refresh after every 30 seconds

Tags:

jquery

refresh

I am experimenting on jQuery and PHP now. And Im having this problem. So I have this code

$(document).ready(function(){
  $('.bopa').load('accounts.php');
});

It works perfectly fine. But the thing is I want the content of the div to refresh every 30 seconds. So that when someone submits something in the database, it will immediately apeear after 20 seconds. Is that possible?

like image 967
Question Man Avatar asked Dec 06 '22 14:12

Question Man


1 Answers

This code will execute the load every 30 seconds.

$(document).ready(function(){
  setInterval(function(){
    $('.bopa').load('accounts.php');
  },30000);
});

The number 30000 is in milliseconds.

1000 milliseconds = 1 second


If you want some more control, for example the ability to disable the refreshes, you can do something like this -

var refreshInterval = '';
$(document).ready(function(){
  refreshInterval = setInterval(function(){
    $('.bopa').load('accounts.php');
  },30000);
});

Then anytime you want to cancel the refresh every 30 seconds you can call -

clearInterval(refreshInterval);

References - window.setInterval (Mozilla Developer Network)

like image 153
Lix Avatar answered Dec 26 '22 01:12

Lix