Sometimes, it becomes necessary to have a section on your web page refresh while the other areas of that page remain static (or dynamic), so here is a little jQuery script to do that.
Here is the jQuery needed to refresh the page:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var refreshPage = setInterval(
function () {
$('#refreshDiv').load('php_test_page2.php');
}, 1000); // refresh every second
</script>
Now, we need to add a div to our page which will be the target of the refresh script:
<div id="refreshDiv"> </div>
We’re almost done. Next we create a page which I called “php_test_page2.php” but you can call it whatever you want. Just ensure that the name you give this page matches the JavaScript above.
In this new page, “php_test_page2.php”, we add the code or text that we want to be refreshed (the example below is merely a timer that is set to display until 4:30 PM today):
<?php
date_default_timezone_set('GMT');
$targetDate = mktime(16, 30, 0, 9, 11, 2012);
$today = time();
$hr = date("H")-4; // optional. Used to change the time to my timezone which is GMT-4.
if ($today < $targetDate) {
print date($hr.":i:s",$today) . '<br />';
}
?>
And there you have it. Hopefully this helps some of you guys out there who need a script to refresh a part of your web page.
Tags: websites