douji1077 2017-11-09 22:40
浏览 54
已采纳

每次将新消息插入DB(MySQL)时刷新chat <div>

I have been searching on the net for a code that refreshes the chat <div> every time a new message is received from the other chat user. I have read about setTimeout() and setInterval() however need more experienced developers advice and help.

The best example is this website. Unless you refresh the browser, you will not see the notification right away. I guess the best example is facebook. Whenever the Database is updated, the notification is popped up right away without refreshing the browser.

Here`s my code.

  1. $check_sql = ("SELECT time from messages where conversation_id = '".$con_id."' ");
  2. $check_query = mysqli_query($con, $check_sql) or die ("ERROR checking:".mysqli_error($con);
  3. while($row = mysqli_fetch_assoc($check_query){
  4. $current_time = date_default_timezone_get();
  5. $message_time_sent = $row['time'];
  6. $cal = $message_time_sent->diff($current_time);
  7. $ans = $cal->format("%D %H:%i:%s");
  8. sscanf($ans,"%d %d:%d:%d ", $day, $hr, $min, $sec);
  9. if (($day == 0) && ($hr == 0) && ($min == 0) && ($sec >0)){
  10. //do something here to get receive message automatically and notification....
  11. }

What I want to achieve:

  1. Facebook automatically receives messages without refreshing the browser. I hope it could be like this.
  2. Automatic pop up of notification without refreshing the browser which I think it is not really important, once we can figure out item 1, then the rest will be all good.

The tricky part about using setInteval() the browser will keep refreshing the <div> chat so when the user input a text to send, it will disappear everytime it refreshes. Please excuse my English....

So, is there a proper way of handling this....

  • 写回答

1条回答 默认 最新

  • dth62818 2017-11-09 23:34
    关注

    setInterval - set interval will call your function every interval you set

    setInterval sample: https://repl.it/OE1o

    <html>
    <body>
        <div id="sample">
            Day Mon DD YYYY HH:MM:SS GMT+XXX (Time Zone)
        </div>
    </body>
    </html>
    
    <script>
    var sample = document.getElementById('sample');
    setInterval(
        // Here is where to do things like dom manipulation
        function() {
            sample.innerHTML = new Date;
        },
    
        // Here is where you set the interval asuming I want to update every 1 second
        1000
    );
    </script>
    

    setTimeout - will be call once the timeout is reached.

    setTimeout sample: https://repl.it/OE3H

    setTimeout(
        // This function will be called once the timeout is reached
        function() {
            console.log('Hello World');
        },
    
        // The timeout is 5 seconds
        5000
    );
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部