代码改变世界

webqq消息提示

2010-08-13 18:59  wlstyle  阅读(418)  评论(0)    收藏  举报
代码

Function.prototype.createDelegate 
= function(scope){
    
var method = this, args = [].slice.call(arguments,1);
    
return function(){
        return method.apply(scope 
|| window, [].slice.call(arguments).concat(args));
    }
}
function TipShow(cfg){
    
//是否是惰性加载    
    this.isLazy = true;
    
//内部使用禁止在初始化传入的参数
    this.protectedProp = {
        timer: 
''
    };
    
if (!this.isLazy) {
        
this.init(cfg);
    }
}

TipShow.defCfg 
= {
    interVal: 
500,//重新赋值的时间
    str: '米兰小铁匠来消息了.米兰小铁匠来消息了.'
}
TipShow.prototype 
= {
    lazyInit: 
function(cfg){
        
this.init(cfg);
        
return this;
    },
    init: 
function(cfg){
        
this.initCfg = {};
        
var defCfg = TipShow.defCfg;
        
for (var key in defCfg) {
            
//如果默认属性中的属性在配置项中 并且该属性不被保护
            if (key in cfg && !(key in this.protectedProp)) {
                
this.initCfg[key] = cfg[key];
            }
else{
                
this.initCfg[key] = defCfg[key];
            }           
            
this[key] = this.initCfg[key];
        }
        
//得到title元素
        this.initTitle =  document.title;
        
//调用render
        this.render();
    },
    
//初始化函數
    render: function(str){
        
//如果传入字符串,认为是要显示不同的内容
        if (!!str && typeof str === "string") {
            
this.str = str;
        }
        
//如果计时器不存在
        if (!this.timer) {
            
this.timer = setInterval(this.doSubStr.createDelegate(this), this.interVal);
        }
        
else {
            
this.cancle();
            
this.render();
        }
    },
    
//字符切割函數
    doSubStr: function(){
        
//如果有要切割的字符存在
        if (!!this.str) {
            
this.setTitle(this.str);
            
this.str= this.str.substr(1+ this.str.charAt(0);
            
        }
        
else {
            
this.cancle();
        }
    },
    
//取消函數
    cancle: function(){
        
//清除计时器
        clearInterval(this.timer);
        
this.timer = null;
        
//重置开始点
        this.startOffset = 0;
        
//重新设置默认的title
        this.setTitle(this.initTitle);
    },
    
//设置头内容
    setTitle: function(title){
       document.title
=title;
    }
    
}

无聊贴一个