Refresh content automatically after some period time – jQuery
Most important method
I’ll be using it in all my examples. First two parameters are required, while third is optional. Remember that 1s = 1000ms.
For example if I want to call function my_function() every 5 seconds (5 seconds * 1000 = 5000ms) I just write it like this:
Refresh whole page
Sometimes we need to refresh whole page. It’s not so common now because it’s timeconsuming and bad for performance, but it needs to be mention. So to refresh page every 5 seconds our code would look like that:
Refresh part of the page
If you need to refresh content in some div of the page you can use jQuery load() function.
For better understanding see example below:
You can see that every 5 sec div with id=”refresh” is updated. Of course our div in html can look like this
<div id="refresh"> <div id="time"> <?php echo date('H:i:s');?> </div> </div>
i used two separate containers so you can see the changes. You can also specify the url for other page, or send some data to it. But this is used more with interaction with user and not with auto refreshing.
If you also want to see something more complicated, check Volume II of ajax auto refresh with working example.