`

定时器 倒计时的一个实现

阅读更多
<html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title> 定时器 倒计时的一个实现 </title>
 </head>
 <body>
 <div id="timerShow" style="width:100px;height:20px;color:red;border:#ccc solid 1px;text-align: center;"></div>
  <script type="text/javascript">
	function Timer(id){
		this.element = document.getElementById(id);   
		this.timer = null;   
		this.count = 0;   
	}   
	Timer.prototype = {   
		begin : function(count,time){
			this.count = count;
			this.time = time || 500;//默认500ms执行一次
			this.show();   
			var _this = this;   
			this.timer = setInterval(function(){_this.show();}, _this.time);   
		}, 
		show : function(){   
			this.element.innerHTML = this.count < 0 ? clearInterval(this.timer) || "0" : this.count--;   
		}
	}

	var t = new Timer("timerShow");
	//倒计时,从60开始,每100ms减1,到0停止
	t.begin(60,100); 
	t.show();
  </script>
</body>
</html>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics