日期:2014-05-16  浏览次数:20377 次

js轮询实现消息提醒
前端文件

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
        <title>消息提醒</title>
</head>
<body>

<div id="hd">

 <ul class="panel">
   	收件箱<span data-type="msg">0</span>
 </ul>
 <ul class="panel3">
   	收件箱<span data-type="msg">0</span>
 </ul>
</div>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
<!--
//收件箱
checkbox = function(url) {
	var box = $('#hd .panel');
	$.getJSON(url, function(data) {
		for (k in data[0]) {
			if (k == 'msg') {
				if (data[0][k] > 0)
					box.find('[data-type=' + k + ']').show().text(data[0][k]);
				else
					box.find('[data-type=' + k + ']').hide();
			}
		}
	});
	setTimeout(function() {
		sbs.checkbox(url)
	}, 10000);
}

checkbox3 = function(url) {
	var box = $('#hd .panel3');
	$.getJSON(url, function(data) {
		alert(data[0]['msg']);
		for (k in data[0]) {
			if (k == 'msg') {
				if (data[0][k] > 0)
					box.find('[data-type=' + k + ']').show().text(data[0][k]);
				else
					box.find('[data-type=' + k + ']').hide();
			}
		}
	});
}

checkbox('server/msg_count.php');
checkbox3('server/msg_count.php');
//-->
</script>



server/msg_count.php
<?php 

	$msgn = array (array ("msg" => 10) );
	echo json_encode ( $msgn );