简体   繁体   English

如何像Gmail的收件箱一样进行AJAX刷新?

[英]How can I make an AJAX refresh like Gmail's inbox?

I'm making a messaging system and I am currently reloading the content of the div holding the messages every 10 seconds using jQuery's .load() , but I have a problem: When trying to make a "Select all" button, "Delete selected" button, etc. when that 10 seconds comes up it reloads the buttons and it reloads the messages, so the messages get deselected because of the reload. 我正在建立一个消息传递系统,我正在使用jQuery的.load()每10秒重新加载包含消息的div的内容,但我有一个问题:当尝试创建“全选”按钮时,“删除所选“按钮等,当10秒钟出现时,它会重新加载按钮并重新加载消息,因此重新加载会取消选择消息。

What I would like to know is how to make it actually load in new messages, but not actually reload the whole div. 我想知道的是如何使它实际加载新消息,但实际上并没有重新加载整个div。 I know that Gmail does not reload the whole div because it works properly. 我知道Gmail不会重新加载整个div,因为它可以正常工作。

This is my JavaScript function that reloads the div and changes the page title (that has inbox count) so it stays updated: 这是我的JavaScript函数,它重新加载div并更改页面标题(具有收件箱计数),因此它保持更新:

function title() {
setTimeout("document.title = $('#heading').text();", 500);
}
function ajaxStuff() {
setTimeout("$('#heading').load('/employee/message/inbox.php #h1_head'); $('#messages').load('/employee/message/inbox.php #messages_inner');title();ajaxStuff();", 10000);
}
ajaxStuff();

Here is how I have the inbox set up: 以下是我设置收件箱的方法:

收件箱截图

Basically what I want to do is load in new messages with AJAX but somehow not refresh the div. 基本上我想要做的是用AJAX加载新消息,但不知何故不刷新div。 I tried looking at Gmail's source but there's too much to go through and they make it confusing with a bunch of random classes and IDs. 我试着查看Gmail的来源,但是有太多的东西需要通过,它们会让一些随机的类和ID混淆。

Note: I have searched this on Google for a while now and did not find anything. 注意:我已经在Google上搜索了一段时间,但没有找到任何内容。

In response to comments: 回应评论:

I don't think a tutorial is warranted here. 我不认为这里有教程。 Change your server code to return the "new" messages with a class="new" attribute, then use: 更改服务器代码以返回带有class="new"属性的“新”消息,然后使用:

$.ajax({
  url: "/employee/message/inbox.php",
  success: function(result) {
    $(result).find(".new").prependTo("#heading");
  }
});

Of course, that code may need some modifications to fit your environment/return data. 当然,该代码可能需要一些修改才能适合您的环境/返回数据。

When checking for new messages send an ID of the newest message in your request. 检查新消息时,请在请求中发送最新消息的ID。 Then your php will return only everything newer that you add to your existing data. 那么你的php将只返回你添加到现有数据中的所有新东西。

    jQuery.ajax({
        type: 'get',
        dataType: 'text',
        url: "/employee/message/inbox.php",
        data: {
            from_user : from_user, 
            to_user: to_user, 
            message_id: message_id,  
            something_else_you_need_to_send: its_value 
            t: Math.random() 
        },
        success: function(data, textStatus){
                      // whatever you need to do with the result returned from php (server)                    
        }

Then in your sql query you do 然后在你的SQL查询中你做

select * from table 
  where user_id=user_id_from_ajax 
    and message_id > message_id_from_ajax`

update 更新

in your php you use 在你使用的PHP中

    $from_user = $_REQUEST['from_user'];
    $to_user = $_REQUEST['to_user'];
    $message_id = $_REQUEST['message_id'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM