douxianglu4370 2014-10-13 14:24
浏览 18

Ajax实时更新跨度

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:

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!

  • 写回答

1条回答 默认 最新

  • dongye7231 2014-10-13 14:35
    关注

    I don't know what your php script returns now, but I would have it return key - value pairs of username slugs and statuses.

    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.

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题