Talha Habib Talha Habib - 6 months ago 12
jQuery Question

refresh container without refreshing page

By using this

<?php echo date('r'); ?>
, is there any way i can refresh simple container (span where my time is echo) without refreshing the page Here this is my page i am displaying date and time here

Answer

Send AJAX request periodically to a PHP file which echos date.

(function worker() {
  $.ajax({
    url: 'backend.php', 
    success: function(data) {
      $('.result').html(data);
    },
    complete: function() {
      // Schedule the next request when the current one's complete
      setTimeout(worker, 5000);
    }
  });
})();

Call this worker() function on the div of the date.

backend.php:

<?php
echo date('Y-m-d H:i:s'); // This is just a sample date format. YOUR DATE FORMAT.
?>