function showUnreadNews()
{
$(document).ready(function() {
$.ajax({
type: "POST",
url: "unread_list.php",
dataType: "json",
success: function(data) {
//alert(msg);
$("#msg").append("data "+data);
}
});
});
}
setInterval('showUnreadNews()',500);//轮询执行,500ms一次
<?php
session_start();
if (empty($_POST['time']))
exit();
set_time_limit(0); //无限请求超时时间
$sae=new SaeMysql();
$sql="SELECT * , COUNT(`chat_id`) FROM `chat` WHERE `is_read` ='0' AND `to_user_id` =".$_SESSION["userid"]." GROUP BY `from_user_id` ";
while (true) {
usleep(500000); //0.5秒
$result=$sae->getData($sql);
if($result)
{
$arr = array('success' => "1", 'result' => $result);
echo json_encode($arr);
exit;
}
else{
$arr = array('success' => "0", 'result' => "");
echo json_encode($arr);
exit();
}
}