<!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>Comet demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="prototype.js"></script>
  </head>
  <body><div id="content">
</div><p>
  <form action="" method="get" onsubmit="comet.doRequest($('word').value);$('word').value='';return false;">
    <input type="text" name="word" id="word" value="" />
    <input type="submit" name="submit" value="Send" />
  </form>
</p><script type="text/javascript">
var Comet = Class.create();
Comet.prototype = {  timestamp: 0,
  url: './backend.php',
  noerror: true,  initialize: function() { },  connect: function()
  {
    this.ajax = new Ajax.Request(this.url, {
      method: 'get',
      parameters: { 'timestamp' : this.timestamp },
      onSuccess: function(transport) {
        // handle the server response
        var response = transport.responseText.evalJSON();
        this.comet.timestamp = response['timestamp'];
        this.comet.handleResponse(response);
        this.comet.noerror = true;
      },
      onComplete: function(transport) {
        // send a new ajax request when this request is finished
        if (!this.comet.noerror)
          // if a connection problem occurs, try to reconnect each 5 seconds
          setTimeout(function(){ comet.connect() }, 5000); 
        else
          this.comet.connect();
        this.comet.noerror = false;
      }
    });
    this.ajax.comet = this;
  },  disconnect: function()
  {
  },  handleResponse: function(response)
  {
    $('content').innerHTML += '<div>' + response['msg'] + '</div>';
  },  doRequest: function(request)
  {
    new Ajax.Request(this.url, {
      method: 'get',
      parameters: { 'msg' : request }
    });
  }
}
var comet = new Comet();
comet.connect();
</script></body>
</html>

解决方案 »

  1.   

    prototype  本来就是JS了 还要弄成什么样的?
      

  2.   

    var Comet = Class.create();
    Comet.prototype = {  timestamp: 0,
      url: './backend.php',
      noerror: true,
    ......Class.create() 是创建一个对象
    Comet.prototype = {...}是给对象添加属性和方法
    可以通过Comet.timestamp调用对象的属性或方法
    Comet.timestamp的值就是0
    Comet.urld的值就是'./backend.php' 以此类推
    如果是方法 通过Comet.connect()可调用Comet的connect方法
      

  3.   

    搞成jquery,prototype.js不熟悉,感觉看天书一样,代码虽然短,但是理解难呼叫高手们。。帮帮忙啊。。我想搞服务器推聊天,所以一定要搞懂啊。