Here's the XHTTP version:
This version is not so memory friendly. Any help getting the memory
usage down would be great.

index.html
========
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/
TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml";>
 <head>
  <title>jQuery Comet demo</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.2.6/jquery.min.js"></script>
 </head>
 <body>
 <div id="comet_div"></div>
 <div id="content"></div>
 <input type="button" id="newframe" value="Start">
 <script type="text/javascript">
  byteoffset = 0;

  var comet =
   {
    load: function()
     {
      xstream  = new XMLHttpRequest();
      xstream.open("GET", "backend.php", true);
      xstream.onreadystatechange = function()
       {
        if(xstream.readyState == 3)
         {
          $("#comet_div").html(xstream.responseText);
         }
       }
      xstream.send(null);
     },

    clearMem: function()
     {
      $("#comet_div").html("");
     },
    time: function(ctime)
     {
      $("#content").html(ctime);
     }
   }

 $(document).ready(function()
   {
    $("#newframe").click(function(){comet.load();});
   });
 </script>
 <body>

backend.php
=========
<?php
function realFlush()
 {
  // echo an extra 250 byte to the browswer - Fix for IE.
  for($i=1;$i<=250;++$i)
   {
    echo ' ';
   }
  flush();
  ob_flush();
 }

set_time_limit(0);
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
realFlush();

$x = 0;
while(1)
 {
  if($x == 10)
   {
    echo '<script type="text/javascript">parent.comet.clearMem();</
script>';
   }
  else
   {
    echo '<script type="text/
javascript">parent.comet.time("'.date('H:i:s').'");</script>';
    sleep(1);
   }
  realFlush();
  ++$x;
 }

Reply via email to