zoukankan      html  css  js  c++  java
  • setTimeout 延时调用

    var cainiao= {
       name:'小马',
       gender : '男',
       showInfo : function(){
    var str = '姓名:' +  this.name ;
        str+=  ',性别:' + this.gender;
       alert(str);
    }
    }
    cainiao.showInfo();

    如果要写一个方法,来实现showInfo的延时调用

    var cainiao= {
       name:'小马',
       gender : '男',
       showInfo : function(){
    var str = '姓名:' +  this.name ;
        str+=  ',性别:' + this.gender;
        alert(str);
       },
       delayInfo: function(){
          // setTimeout('alert(this.cainiao.name)',1000);//正确
        setTimeout('this.cainiao.showInfo()',1000);// 正确
    //setTimeout('this.showInfo()',1000);//  错误
      }  
    }
    cainiao.delayInfo();
    //上面是对象实例


    //下面是构造函数
    window.objTimeoutFuncs  = [];
    window.objTimeoutIds =  [];
    function  objTimeout(obj,func,time,id){
    window.objTimeoutFuncs[id] = function(){
      func.call(obj);
    };
    window.objTimeoutIds[id] =  setTimeout('objTimeoutFuncs[\''+ id +'\']()',time);
    }
    function  clearObjTimeout(id){
    clearTimeout(window.objTimeoutIds[id]);
    }
    function  Person(name_,gender_){
    this.name = name_;
    this.gender = gender_;
    this.showInfo = function(){
      var str = '姓名:' +  this.name ;
      str+=  ',性别:' + this.gender;
      alert(str );
    };
    this.delayInfo = function(){
    objTimeout(this,this.showInfo,2000,this.name);//
      //setTimeout("this.showInfo()",1000);//出错
      //setTimeout.call(this,'this.showInfo()',1000);//出错
    };
    }
    var p1=new Person("小马","男");
    p1.delayInfo();

  • 相关阅读:
    BZOJ1862: [Zjoi2006]GameZ游戏排名系统
    BZOJXXXX: [IOI2000]邮局——四边形不等式优化初探
    BZOJ1801: [Ahoi2009]chess 中国象棋
    BZOJ1505: [NOI2004]小H的小屋
    BZOJ1899: [Zjoi2004]Lunch 午餐
    BZOJ1057: [ZJOI2007]棋盘制作
    BZOJ1100: [POI2007]对称轴osi
    BZOJ1123: [POI2008]BLO
    线性规划之单纯形讲解
    BZOJ 3265 志愿者招募加强版(单纯形)
  • 原文地址:https://www.cnblogs.com/oneroom/p/settimeoutDelayCall.html
Copyright © 2011-2022 走看看