【问题标题】:jquery typing effectjquery打字效果
【发布时间】:2012-01-30 07:52:33
【问题描述】:

我只是想知道你们可以为我尝试采用的方法推荐/建议什么。我正在尝试制作自己的简单实时支持聊天系统,并且需要为客户端制作一个功能,以便在代理键入消息时查看。我已经想出了如何为自己打字的人而不是为远程的人这样做。

请帮忙,因为我不擅长 jQuery!这是我当前的代码,当您在 ID 为#chatMessage 的输入字段中键入时,它只是显示。请注意,这是一个 PHP、MySQL 和 jQuery 聊天系统。

$('#chatMessage').keyup(function(){
    if ($("#chatMessage").val() == ''){
        $('#typing').html('');
    }else{
        $('#typing').html('The agent is typing a message..');
    }
});

谢谢

【问题讨论】:

    标签: php jquery mysql html chat


    【解决方案1】:

    您只需要向服务器添加一个 ajax 调用,然后在客户端上添加一个计时器来检查代理状态...

    //代理端..

    function checkStatus(){
          jQuery.get('/server-url?agent_id=32&status', function(data){
               if (data.status == 'typing')
                   $('#typing').html('The user/client is typing a message..');
               else
                   $('#typing').html('');
               checkStatus();
          });
    }
    // Start the function begining. 
    setTimeout(checkStatus, 300);
    var agent_timer;
    $('#chatMessage').keyup(function(){ 
       if (agent_timer) 
            clearTimeout(agent_timer);
    
       if ($("#chatMessage").val() == ''){
          status = 'empty'; 
       } else { 
          status = 'writing';
       }
       agent_timer = setTimeout(function(){
           // Send status to server php script
           $.post('/server-url?agent_id=23', {status: status}, function(data){
               // handle response
           });
       }, 400);
    });
    

    // 服务器端...

    // check for agent, and other things...
    if (isset($_GET['agent_id'])){
        // Agent server side
        if (isset($_POST['status']){
            // Save status to database
            $this->save(array('agent' => $agent, 'chat_id' => $chat_id, 'status' => $_POST['status']));
        }
        if (isset($_GET['status'])){
            $data = $this->read(array('agent_id' => $chat_id));
        }
    } else {
        // This is the client server side
        if (isset($_GET['status'])) {
             $data = $this->read(array('chat_id' => $chat_id));
             return $data['status'];
        }
    }
    // handle other things and return data if necessary
    // echo json_encode($data);
    

    // 客户端JS

    function checkStatus(){
          jQuery.get('/server-url?chat_id=32&status', function(data){
               if (data.status == 'typing')
                   $('#typing').html('The agent is typing a message..');
               else
                   $('#typing').html('');
               checkStatus();
          });
    }
    
    // Start the function at begining.
    setTimeout(checkStatus, 300);
    // We can do the same, so the agent can know if user is typing too (you must also copy the above function)
    var client_timer;
    $('#chatMessage').keyup(function(){ 
       if (client_timer) clearTimeout(client_timer);
       if ($("#chatMessage").val() == ''){
          status = 'empty'; 
       } else { 
          status = 'writing';
       }
       client_timer = setTimeout(function(){
           // Send status to server php script
           $.post('/server-url?chat_id=23', {status: status}, function(data){
               // handle response
           });
       }, 400);
    });
    

    也许有比在 mysql 中更新一行更好的方法...但我现在想不出一个..

    【讨论】:

    • +1 :) 谢谢!我实际上想过让它像这样更新一个 mysql 行,但不确定它是否会在服务器上造成太多负载以及什么不是,但我想它不会。
    • 一个问题,我需要做什么来做一个计时器,这样每次击键都不会调用php?
    • 我已将 setTimeout 和 clearTimeout 函数添加到脚本中。看看;)
    • 你用什么类来处理服务器端的数据库?
    • 接受答案..来吧,你可以做到的;)
    猜你喜欢
    • 2020-04-02
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2018-03-14
    相关资源
    最近更新 更多