sven
4/3/2014 - 10:47 PM

Simple Ajax Polling Demo

Simple Ajax Polling Demo

{
  "live": false,
  "info": "Online!"
}
<!DOCTYPE html>
<head>
  <title>Ajax Polling</title>
  <meta charset="UTF-8" />
</head>
<body>
  <p>Status: <span class="status"></span></p>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script>
    (function() {
      var status = $('.status'),
        poll = function() {
          $.ajax({
            url: 'status.json',
            dataType: 'json',
            type: 'get',
            success: function(data) { // check if available
              status.text('Offline!');
              if ( data.live ) { // get and check data value
                status.text(data.info); // get and print data string
                clearInterval(pollInterval); // optional: stop poll function
              }
            },
            error: function() { // error logging
              console.log('Error!');
            }
          });
        },
        pollInterval = setInterval(function() { // run function every 2000 ms
          poll();
          }, 2000);
        poll(); // also run function on init
    })();
  </script>
</body>
</html>

Ajax Polling