如何在线刷新用户总数?

时间:2016-05-22 11:30:51

标签: javascript php jquery

我使用民意调查从服务器轮询msg,但是如何增强它以使整个用户在线也会自动刷新?现在我需要刷新浏览器来改变在线用户总数

这是我的HTML

<form name ="chatroom">

    <div class="chatroom-upper-info">
        <div class="left-total-user-online" id="left-total-user-online">
            total user online here.....
        </div><div class="load-more-data">
            <input type="button" class="btn btn-link" id="load" value="Load More Results">
        </div>
    </div>

    <div class="chatroom-upper-container" id="chatroom-upper-container">
        <div id="inner">
            Loading Message....<img src="../images/loading.gif"/>
        </div>
         <input type="hidden" id="more_message" value="40">
    </div>
    <div class="chatroom-lower-left-container">
        <textarea class="message-setting" id="area-message" placeholder="type text" name= "msg"></textarea>
    </div><div class="chatroom-lower-right-container">
        <button type="button" class="btn sendmessage-btn" onclick= "submitChat()">Send</button>
    </div>  

</form>

这是我的js

function submitChat(){
if(chatroom.msg.value == '' ){
 alert('You didnt input any message');
return;
}

 var msg = chatroom.msg.value;
 var xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function(){
  if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
  document.getElementById('inner').innerHTML = xmlhttp.responseText;
       }
   }
   xmlhttp.open('GET','chatinsert.php?&msg='+msg, true);
   xmlhttp.send();
   $('.chatroom-upper-container').scrollTop($('.chatroom-upper-container').get(0).scrollHeight);
   }

   $(document).ready(function(e){

  $.ajaxSetup({cache:false});
  setInterval(function(){$('#inner').load('chatlogs.php');}, 2000);
  setInterval(function(){$('#left-total-user online').load('user_online.php');}, 2000);

$(".sendmessage-btn").click(function(){
    $("#area-message").val('');     
 });     
});

 (function poll() {
   setTimeout(function() {
    $.ajax({ 
   url: "http://localhost/pme/main/newchatroom.php", 
   success: function(data) 
   {
        setValue(data.value);
   }, 
   dataType: "json", 
   complete: poll 
   });
}, 
30000
);  
});

这是我的php for count total user online

<?php 

    include '../config.php';

    $sql= mysqli_query($connection,' select * from session');
    $row = mysqli_num_rows($sql)

?>

更新

这似乎很有效,但我的页面没有显示任何内容。任何解决方案?

1 个答案:

答案 0 :(得分:0)

使用setTimeout javascript函数并发送ajax请求以定期返回活动会话计数。这将在后台进行,您无需重新加载页面。如果你想重新加载页面,你可以使用setTimeout来完成它。

<script type="text/javascript">
  setTimeout(function(){
     location = ''
  },60000)
</script>