本帖最后由 ldwd 于 2011-12-27 16:44:35 编辑

解决方案 »

  1.   

    this.showtip = showtip;   //将属性赋值。
    这里的this.showtip只属于第一次进入的show函数空间,即使你后面把他传递给了setTimeout中的第二个show函数,但只是传了个变量的指针,等setTimeout执行第二个show函数的时候,第一个show函数的空间已经消失,它的this.showtip变量也消失,既然这个this.showtip指针没有指向任何东西,当然就变成null了
      

  2.   

    this.showtip = showtip;   //将属性赋值。
    这里的this.showtip只属于第一次进入的show函数空间,即使你后面把他传递给了setTimeout中的第二个show函数,但只是传了个变量的指针,等setTimeout执行第二个show函数的时候,第一个show函数的空间已经消失,它的this.showtip变量也消失,既然这个this.showtip指针没有指向任何东西,当然就变成null了
      

  3.   

    那要如何修改这段代码呢?是不是setTimeout函数没法在封装的类中使用?
      

  4.   

    var obj = this;
    this.show = function( )    //定时循环的函数
        { 
            
            alert(typeof obj.showtip);
            
            if(obj.timerRunning)
                clearTimeout(obj.timerID);
            
            obj.timerRunning = false
            var now = new Date();
            var hours = now.getHours();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();
            var timeValue = "" + ((hours < 10) ? "0" : "") + hours;
            timeValue += ((minutes < 10) ? ":0" : ":") + minutes
            timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
            
            if(showtip != null)
            {
                obj.showtip.innerHTML = timeValue;
            }
                
            obj.timerID = setTimeout("show()" ,1000);   
            obj.timerRunning = true;
        }
      

  5.   

    我找到了解决方法,方法如下即可:this.showtip = null;
    this.timerID = null;
       this.timerRunning = false;
      
    // Make sure the clock is stopped

    this.show = function()
    {
    var obj = this;

    //alert(typeof obj.showtip);

    if(obj.timerRunning)
    clearTimeout(obj.timerID);

    obj.timerRunning = false;

    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    var timeValue = "" + ((hours < 10) ? "0" : "") + hours;
    timeValue += ((minutes < 10) ? ":0" : ":") + minutes
    timeValue += ((seconds < 10) ? ":0" : ":") + seconds;

    if(obj.showtip != null)
    obj.showtip.innerHTML = timeValue;

    obj.timerID = setTimeout(function(){obj.show();},1000);
    obj.timerRunning = true;
    }
    把四楼的var obj = this移到函数内,并将setTimeout第一个参数改为函数,即可完美解决。