1

试图为我的网站创建一个 AJAX IM...需要在将行插入 mysql DB 时加载页面的一部分...任何人都可以帮我解决这个问题..提前谢谢

4

3 回答 3

1
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
var  waittime=2000;
var intUpdate = null;


function verifDB(){

$.ajax({
   type: "POST",
   url: "verifdb.php",
   success: function(msg){
    alert(msg),; 
   }
 });

intUpdate = setTimeout("verifDB()", waittime);
}
verifDB();
</script>

每 2000 毫秒查询一次 verifdb.php 文件以检查数据库,您可以将文件放在 requette verifdb.php 中,您将在变量 msg 中得到答案

于 2012-04-05T09:52:33.090 回答
0

客户端

对于客户端的异步请求,您可以使用 JQuery 或纯 Javascript XMLHTTPRequest

服务器端

我知道您已经指定了 PHP,但我建议您检查google 频道的工作方式并在 PHP 中进行类似的实现。

由于检查有多个用户检查数据库上的更新,我建议您使用 memcache。

就像是:

$script_called_time = time();
while($memcache->get('last_message') < $script_called_time){
    usleep(100);
}

$result = $database->query("SELECT * FROM `messages` WHERE `date` > " . $script_called_time . "'");
...

这样将建立连接,并且用户将在有任何响应时收到响应...

于 2012-04-06T00:07:48.367 回答
0
(function() {
  var chat = {
    messageToSend: "",
    messageResponses: [
      "I Love You",
      "I Wants to Kiss You.",
      'Hug Me!"',
      "Lets Sleep Together",
      "Lets go for a date",
      "Will you be physical with me?"
    ],
    init: function() {
      this.cacheDOM();
      this.bindEvents();
      this.render();
    },
    cacheDOM: function() {
      this.$chatHistory = $(".chat-history");
      this.$button = $("button");
      this.$textarea = $("#message-to-send");
      this.$chatHistoryList = this.$chatHistory.find("ul");
    },
    bindEvents: function() {
      this.$button.on("click", this.addMessage.bind(this));
      this.$textarea.on("keyup", this.addMessageEnter.bind(this));
    },
    render: function() {
      this.scrollToBottom();
      if (this.messageToSend.trim() !== "") {
        var template = Handlebars.compile($("#message-template").html());
        var context = {
          messageOutput: this.messageToSend,
          time: this.getCurrentTime()
        };

        this.$chatHistoryList.append(template(context));
        this.scrollToBottom();
        this.$textarea.val("");

        // responses
        var templateResponse = Handlebars.compile(
          $("#message-response-template").html()
        );
        var contextResponse = {
          response: this.getRandomItem(this.messageResponses),
          time: this.getCurrentTime()
        };

        setTimeout(
          function() {
            this.$chatHistoryList.append(templateResponse(contextResponse));
            this.scrollToBottom();
          }.bind(this),
          1500
        );
      }
    },

    addMessage: function() {
      this.messageToSend = this.$textarea.val();
      this.render();
    },
    addMessageEnter: function(event) {
      // enter was pressed
      if (event.keyCode === 13) {
        this.addMessage();
      }
    },
    scrollToBottom: function() {
      this.$chatHistory.scrollTop(this.$chatHistory[0].scrollHeight);
    },
    getCurrentTime: function() {
      return new Date()
        .toLocaleTimeString()
        .replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3");
    },
    getRandomItem: function(arr) {
      return arr[Math.floor(Math.random() * arr.length)];
    }
  };

  chat.init();

  var searchFilter = {
    options: { valueNames: ["name"] },
    init: function() {
      var userList = new List("people-list", this.options);
      var noItems = $('<li id="no-items-found">No items found</li>');

      userList.on("updated", function(list) {
        if (list.matchingItems.length === 0) {
          $(list.list).append(noItems);
        } else {
          noItems.detach();
        }
      });
    }
  };

  searchFilter.init();
})();

Messenger 使用 Jquery 和 PHP 如果您需要有关此答案的任何帮助,请随时通过 pachauriashokkumar[at]gmail[dot]com 与我联系

需要外部文件

  1. https://code.jquery.com/jquery-3.4.1.js
  2. https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js
  3. https://raw.githubusercontent.com/javve/list.js/v1.5.0/dist/list.min.js

使用 JQuery 和 PHP 演示的 Messenger也在这里PenCode上这篇文章的作者可通过电子邮件 pachauriashokkumar[at]gmail[dot]com 进行澄清

于 2020-03-23T21:29:03.230 回答