• 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;
    }


  • 相关阅读:
    C++ string用法
    C++ 静态变量及函数的生命周期
    C++ const的用法和作用
    C++ 指针和引用的区别
    C++ struct 和 Class的区别
    C++对象模型-构造函数语意学
    大端模式与小端模式、网络字节顺序与主机字节顺序
    Spring Boot系列——Spring Boot如何启动
    分库分表利器——sharding-sphere
    并发和多线程-八面玲珑的synchronized
  • 原文地址:https://www.cnblogs.com/lfzwenzhu/p/1749121.html
Copyright © 2020-2023  润新知