简体   繁体   English

Ajax实时更新跨度

[英]Ajax update a span real time

I'm trying to get a online/offline status update for a chatsystem. 我正在尝试获取聊天系统的在线/离线状态更新。 Currently I'm stuck on how to update a span in real time. 目前,我停留在如何实时更新跨度上。 I got things working but it keeps adding the value (person which is online) to my span. 我的工作正常,但它不断增加价值(在线用户)。 What I want to achieve is to have a static list of people (those who are inside my database), and next to it a green bullet if they are online. 我想要实现的是有一个静态的人员列表(位于数据库中的人员),如果他们在线,则旁边有一个绿色的项目符号。

This is my jQuery script: 这是我的jQuery脚本:

function theStatus(type, msg){
    $("#status").append(
        "<span class='status "+ type +"'>"+ msg +"</span>"
    );
}

function getStatus(){
$.ajax({
    type:       "get",
    url:        "php/ajax/status_users.php",

    async:      true,
    cache:      false,
    timeout:    1000,

    success: function(data){
        theStatus("rt", data);
        setTimeout(
            getStatus,
            1000
        );
        } 
    });
};


$(document).ready(function(){
    getStatus();
});

All help is welcome! 欢迎所有帮助!

I don't know what your php script returns now, but I would have it return key - value pairs of username slugs and statuses. 我不知道您的php脚本现在返回什么,但是我会让它返回键-用户名和状态的值对。

Then you could add the username slug as an ID to each user span and target the correct span to change the status if necessary or to add an additional user if it is not found in the html. 然后,您可以将用户名slug作为ID添加到每个用户范围,并定位正确的范围以在必要时更改状态,或者在html中找不到该用户时添加其他用户。

At the moment you are simply appending span 's each time your ajax function returns a result instead of checking the existing list against the returned list from the server. 目前,您只需在ajax函数每次返回结果时附加span而不用对照服务器返回的列表检查现有列表。

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

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