1

I want to reload my table.php every 3 seconds. I wrote a working code, but when I call the page manual, or refresh it manually, it starts to load my content after 3 seconds, and not instantly. Anyone now, how to call first table.php, and refresh after it every 3 seconds like in my code below?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      setInterval(function () {
        $('#show').load('table.php')
      }, 3000);
    });
  </script>
2
  • 1
    Call $('#show').load('table.php'). Commented Jan 9, 2018 at 20:01
  • Time to consider websockets to reduce server load for that amount of traffic Commented Jan 9, 2018 at 20:06

3 Answers 3

2

There's no way to call function immediately except just call it:

$(document).ready(function() {
  $('#show').load('table.php');

  setInterval(function () {
    $('#show').load('table.php')
  }, 3000);
});

Going further you can encapsulate same calls to a function:

$(document).ready(function() {
  function loadTable() {
      $('#show').load('table.php');
  }
  loadTable();

  setInterval(loadTable, 3000);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Call the table load right away, then include in your loop:

$(document).ready(function() {
    $('#show').load('table.php')
    setInterval(function () {
        $('#show').load('table.php')
    }, 3000);
});

Comments

-1

A small optimization, the table will be filled exactly after 3 seconds before the previous loading, check the callback:

var reload = function() {
    $('#show').load('table.php', function() {
        // Start the counter when the data is loaded... (to avoid problems when table.php takes more than 3 secs to return the data)
        setTimeout(reload, 3000);
    });
};
// Fill the table immediately
reload();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.