最近在做公司的项目的用到了 类似开心网上新消息的功能,本人写了一个js的函数实现此功能,供大家交流:-)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>测试页面</TITLE>
<script type="text/javascript">
var dynamicMsg = null;
function init() {
dynamicMsg = new DynamicMessage(window.document.title, "【HI,消息】", "【 】");
}
function begin() {
dynamicMsg.initIntervalMsg();
}
function end() {
dynamicMsg.clearIntervalMsg();
}
/**
* 处理新消息提示的操作
*/
function DynamicMessage(defaultMsg, msg, hiddenMsg) {
this.initIntervalMsg = function() {
this.intervalMsg = setInterval(function() {
if(!this.bMsg) {
window.document.title = msg + " - " + defaultMsg;
this.bMsg = true;
} else {
window.document.title = hiddenMsg + " - " + defaultMsg;
this.bMsg = false;
}
},
1000
);
};
this.clearIntervalMsg = function() {
if(this.intervalMsg != null) {
clearInterval(this.intervalMsg);
window.document.title = defaultMsg;
this.bMsg = false;
}
};
}
</script>
</HEAD>
<BODY οnlοad="init();">
<center>
<input type="button" value="begin" οnclick="begin();"/>
<input type="button" value="end" οnclick="end();"/>
<br/>
coloryeah - 为意识增添色彩!
</center>
</BODY>
</HTML>
由于这个对象没有初始化太多次和涉及闭包的操作,故没有采用prototype原型。
好啦,一个简单实用的 hi,消息 完成了。