title: jquery long polling: prevent update until data actually changes
body: <p>I have created a script that long polls a JSON source and updates a div with the results. My code is posted below. </p>
  
  <p>The code works fine and the data I request is grabbed and outputted correctly. The code checks the JSON and grabs 2 images and some text data. </p>
  
  <p>The problem is it keeps refreshing the data and downloading the images constantly causing high server load and bandwidth consumption (obviously this is small right now but will increase as I complete my project). This also leads ot me not being able to select the text in the div and the images flicker as they are reloaded, both are undesirable consequences of my current code.  </p>
  
  <p>I want to be able to grab the data and display it, and not update it at all until the data actually changes in the JSON response, I am assuming I need to do something with a timestamp?. I have tried creating a lastupdate timestamp by using the first_aired key from the JSON but it is not working, I am unsure if I have made a mistake or if I am barking up the wrong tree. </p>
  
  <p>Could someone take a look at the code I have and perhaps point me in the correct direction as to what I need to do? </p>
  
  <pre><code>var lastupdate = 0;
  // call getData when the document has loaded
  $(document).ready(function(){
  getData(lastupdate);
  });
  var getData = function(lastupdate) {
  $.ajax({
    type: "GET",
    url: 'http://api.trakt.tv/user/watching.json/apikey/user/lastupdate='+lastupdate+'&amp;callback=?',
    dataType: 'jsonp',
    async: true,
    cache: false,
    // timeout after 5 minutes, shut process down and restart
    timeout:300000,
    // process a successful response
    success: function(watching_now) {
      if (!jQuery.isEmptyObject(watching_now)) {
      //console.log(watching_now);
      var airDate = watching_now.episode.first_aired;
      var showTitle = watching_now.show.title;
      var showPoster = watching_now.show.images.poster;
      var showURL = watching_now.show.url;
      var episodeTitle = watching_now.episode.title;
      var episodeScreen = watching_now.episode.images.screen;
      var episodeNumber = watching_now.episode.number;
      var episodeSeason = watching_now.episode.season;
      $('#watching-now').html('&lt;div class="screencap"&gt;&lt;img src="' + episodeScreen +' " width="240" height="150" /&gt;&lt;/div&gt;&lt;div class="poster"&gt;&lt;a href="'+showURL+'" target="_blank"&gt;&lt;img src="' + showPoster +'" width="85" height="120" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="watching-info"&gt;'+episodeSeason+'x'+episodeNumber+' - '+episodeTitle+'&lt;/div&gt;')
      }
      else {
      $('#watching-now').html('You are not currently watching anything')
      }
      // set lastupdate
      lastupdate = watching_now.airDate;
      // call again in 1 second
      setTimeout('getData('+lastupdate+');', 1000);
      },
      // handle error
    error: function(XMLHttpRequest, textStatus, errorThrown){
      // try again in 10 seconds if there was a request error
      setTimeout('getData('+lastupdate+');', 10000);
  },
  });
  };
  </code></pre>
  
  <p>Here is the JSON I am getting the information form:</p>
  
  <pre><code>{"type":"episode","action":"watching","show":{"title":"Stargate Atlantis","year":2004,"url":"http://trakt.tv/show/stargate-atlantis","imdb_id":"tt0374455","tvdb_id":"70851","tvrage_id":"5324","first_aired":1089961200,"country":"United States","overview":"The story of Stargate Atlantis follows the cliffhanger episode on Stargate SG-1's seventh season finale \"Lost City\", where SG-1 found an outpost made by the race known as the Ancients in Antarctica. After the events of Stargate SG-1 season eight premiere \"New Order\", the Stargate Command sends an international team to investigate the outpost. Soon, Dr. Daniel Jackson discovers the location of the greatest city created by the Ancients, Atlantis. The story unfolds when the members of the expedition encounter the Wraith, the race that defeated the Ancients ten thousand years ago.","runtime":60,"network":"Syfy","air_day":"Monday","air_time":"9:00pm","certification":"TV-PG","images":{"poster":"http://trakt.us/images/posters/329.3.jpg","fanart":"http://trakt.us/images/fanart/329.3.jpg","banner":"http://trakt.us/images/banners/329.3.jpg"},"genres":["Action","Adventure","Science Fiction"]},"episode":{"season":3,"number":10,"title":"The Return (1)","overview":"The Atlantis expedition is stunned to learn that a ship full of Ancients is returning to reclaim their lost city. ","first_aired":1158908400,"url":"http://trakt.tv/show/stargate-atlantis/season/3/episode/10","images":{"screen":"http://trakt.us/images/episodes/329-3-10.3.jpg"}}}
  </code></pre>
  
  <p>If you need more information please just ask and I will provide everything I can. </p>
  
  <p>Cheers</p>