title: Ajax Refresh Div Only If Variable Changed
body: <p>I have a div that reloads data from another page. The code looks like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var auto_refresh = setInterval(
    function() {
      $('#mydiv').load('load.php');
    }, 1000); // refresh every 1000 milliseconds</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.js"&gt;&lt;/script&gt;
  &lt;div id="mydiv"&gt;
    &lt;?php echo 'loading...';?&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>This works wonderfully to refresh my data. However, I don't really want it to refresh the div every 1000 milliseconds, I want it to check to see if anything has changed, and if so then refresh the div.</p>
  
  <p>For example:</p>
  
  <pre><code>$current_vericode = 'kdsjfkdfj';
  
  $sql = "SELECT\n".
  "   vericode\n".
  "FROM\n".
  "   users\n".
  "WHERE\n".
  "   users.id = 3";
  
      $result = $conn-&gt;query($sql);
  
      if ($result-&gt;num_rows &gt; 0) {
          while ($row = $result-&gt;fetch_assoc()) {
              $new_vericode = $row["vericode"];
          }
      }
  
  if ($current_vericode == $new_vericode)
  {
      //don't refresh the div
  } else {
      //refresh it and...
      //$current_vericode = $new_vericode
  }
  </code></pre>
  
  <p>This way all that is happening is that it checks mysql to see if the specified field has changed. If it hasn't then it doesn't do anything. If it has then it will refresh the div and update the code.</p>
  
  <p>I understand that the effect will be the same as constantly updating, but I really want to add forms and stuff which I could do with it not updating until the mysql field has changed. </p>
  
  <p>So every 1000 milliseconds, check mysql and compare it to current value. If they are the same then end / exit. If they have changed then refresh and save new value as current. </p>
  
  <p>Any ideas?</p>