Javascript中的 setTimeout函数在类中的应用

本文详细介绍了JavaScript中定时器的使用方法及注意事项,通过一个具体的示例讲解了如何正确设置和使用setTimeout函数,并解释了常见的错误及其原因。

今天写一个类,在类中使用到了setTimeout函数。

1。 this.timerID = setTimeout("showtime()",1000);

提示:showtime is not defined

2。 this.timerID = setTimeout("this.showtime()",1000);

提示:this.showtime is not a function

3。 this.timerID = setTimeout("this.showtime",1000);

没有错误,但是不执行。

4. 正确的方法:

  var self = this;

  this.timerID = setTimeout(function(){self.showtime();},1000);

在方法1 - 3 中找不到showtime()函数。

方法1, showtime指的是一个单独的函数。

方法2, 3,this 指向的是window对象,而不是当前实例对象。

 

类如下:

 

ExpandedBlockStart.gif代码
// JScript File
function clock()
{
  
this.timerID = null;
    
this.timerRunning = false;
    
this.startHour = 0;
    
this.startMinute = 0;
    
this.startSecond = 0;
    
this.showObj = null;
    
    
if(typeof(clock._initialized) == "undefined")
    {
      clock.prototype.init = function(otimerID,timerRunning,startHour,startMinute,startSecond,showObj){
        
this.timerID = otimerID;
        
this.timerRunning = timerRunning;
        
this.startHour = startHour;
        
this.startMinute = startMinute;
        
this.startSecond = startSecond;
        
this.showObj = showObj;
        };
      clock.prototype.toggletimer = function(obj){
          
if (this.timerRunning)
          {
            obj.value = "Start Timer";
            
this.stopclock ();
          }
          
else
          {
            obj.value = "Stop Timer";
            
this.startclock ();
          }
        };        
      
// stop the clock
      clock.prototype.stopclock = function(){
        
if (this.timerRunning)
          clearTimeout(this.timerID);
          
this.timerRunning = false;
       };
      
// start the clock
      clock.prototype.startclock = function(){
        
var now = new Date();
        
this.startHour = now.getHours();
        
this.startMinute = now.getMinutes();
        
this.startSecond = now.getSeconds();
        
// Make sure the clock is stopped
        this.stopclock();
        
this.showtime();
      };
      
// actually display the time
      clock.prototype.showtime = function(){
        
var i = 0;
        
var strSec = "";
        
var now = new Date();
        
var hours = now.getHours();
        
var minutes = now.getMinutes();
        
var seconds = now.getSeconds();
        
var timeValue = "" + ((hours >12? hours -12 :hours);
        timeValue += ((minutes < 10? ":0" : ":"+ minutes;
        timeValue += ((seconds < 10? ":0" : ":"+ seconds;
        timeValue += (hours >= 12? " P.M." : " A.M.";
        
        
var MinutesElapsing = Math.round(((hours - this.startHour)*60 + (minutes - this.startMinute) + (seconds-this.startSecond)/60)*10)/10;
        
this.showObj.value = MinutesElapsing;
        
        
for (i = 0; i < seconds-this.startSecond; i++) {
          strSec = strSec + "=";
        };
        window.status = timeValue;
        
var self = this;
        
this.timerID = setTimeout(function(){self.showtime();},1000);
        
this.timerRunning = true;
      };
    }
 clock._initialized = true;
}


转载于:https://www.cnblogs.com/lfzwenzhu/archive/2010/06/01/1749121.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值