1. 创建两个互调用函数 setTitle1和setTitle2
2. 定时调用机制
3. 窗体获取焦点和失去焦点存状态
4. 来消息了存状态(在本例中,模拟每3秒来1个消息)
5. 在setTitle1中判断状态
完整代码:
<script src="jquery-1.10.2.js" ></script>
<div class="pnl"></div>
<script>
var newMsg = false;
var window_focused = true;
setInterval(function(){
$(".pnl").append("<div>new msg</div>");
newMsg = true;
}, 3000);
function setTitle1(){
if(newMsg === true && window_focused === false){
document.title = "New Msg";
setTimeout(setTitle2,100);
}
}
function setTitle2(){
document.title = "";
setTimeout(setTitle1,100);
}
setInterval(setTitle1, 1000);
$(window).focus(function (e) {
window_focused = true;
});
$(window).blur(function (e) {
window_focused = false;
});
</script>