zoukankan      html  css  js  c++  java
  • 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对象,而不是当前实例对象。

    类如下:

    代码
    // 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;
    }


  • 相关阅读:
    在vm中安装ubuntu 11 并部署 xampp集成环境步骤
    修改xampp的mysql默认密码
    linux下重命名文件的命令
    IE6下DIV最小高度不能为0的解决方法
    linux下解压tar.gz 文件和 tar.bz2 文件命令
    linux svn 使用
    win7 下设置java环境变量
    在linux下安装svn软件rabbitvcs
    linux下开启ssh服务
    【转】DataSet与DataTable的区别
  • 原文地址:https://www.cnblogs.com/lfzwenzhu/p/1749121.html
Copyright © 2011-2022 走看看