倒数计时类

倒数计时类

 

< html >
< head >
< script  type ="text/javascript" > ...
var Class = ...{
  create: 
function() ...{
    
return function() ...{
      
this.initialize.apply(this, arguments);
    }

  }

}


//倒数计时类
var Countdown = Class.create();
Countdown.prototype 
= ...{
  initialize: 
function(callback, frequency, elm) ...{
    
this.obj = document.getElementById(elm) || null;
    
this.callback = callback;
    
this.frequency = frequency;
    
this.registerCallback();
  }
,
  
//设置回调
  registerCallback: function() ...{
    
var oCountdown = this;
    
this.timer = setInterval(function()...{ oCountdown.onTimerEvent(); }1000);
  }
,
  
//停止倒数
  Stop: function() ...{
    clearInterval(
this.timer);
  }
,
  
//设置回调函数
  onTimerEvent: function() ...{
    
if (this.frequency-- <= 0...this.Stop(); this.Inset(0); this.callback(); }
    
else ...this.Inset(this.frequency); }
  }
,
  
//把剩余时间显示到相应标签
  Inset: function(n) ...{
    
if(this.obj) this.obj.innerHTML = n;
  }

}

script >
head >
< body >
倒数剩下
< span  id ="timeout" > 6 span > 秒。
< script  type ="text/javascript" > ...
var timer = new Countdown(function()...{alert("时间到了")}6'timeout');
script >
< input  name =""  type ="button"  value ="Stop"  onClick ="timer.Stop();" >
body >
html >


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部