Dev-Ria
Dev-Ria

Reputation: 329

jquery counter stops after page refresh

my code for live counter of a table.

jQuery(function($){
  setInterval(function(){
    $.get( 'counter.php', function(newRowCount){
      $('#mycounter').html( newRowCount );
    });
  },1000); // 1000ms == 1 seconds
});

<span id='mycounter'></span>

counter.php

include 'mysqli.php';

$result = $t->query("SELECT count(*) AS c FROM data");

$row = $result->fetch_assoc();

echo $row['c'];

but after i refresh the page or go to another page and come back it doesnt work. It gives me a blank with no counts. I think its due to the cache of the page because if I open it in incognito I don't have this issue.

whats a work around?

Upvotes: 0

Views: 179

Answers (1)

adeneo
adeneo

Reputation: 318182

setting the cache option to false :

jQuery(function($){
  setInterval(function(){
      $.ajax({
          url   : 'counter.php',
          cache : false
      }).done(function(newRowCount) {
          $('#mycounter').html( newRowCount );
      });
  },1000); // 1000ms == 1 seconds
});

Upvotes: 1

Related Questions