迟到的春天

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

知识点:

1、setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。

2、clearInterval() 方法可取消由 setInterval() 设置的 timeout。

    clearInterval() 方法的参数必须是由 setInterval() 返回的 ID 值。

 

代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>倒计时</title>
</head>
<script>
var timer = function(id)
{
this.objTimer = document.getElementById(id);
}
timer.prototype
=
{
init :
function ()
{
var _self = this;
if(!this.objTimer)
return;
this.times = parseInt(this.objTimer.innerHTML);
if(this.times <= 0)
{
this.times = 0;
}
this.setTimer();
this.interval = setInterval(function(){_self.setTimer.call(_self)},1000);

},
setTimer :
function ()
{
if(this.times <=0)
{
clearInterval(
this.intervar);
return;
}
this.times--;
var timeH = parseInt(this.times / 3600);
var timeM = parseInt(this.times % 3600 / 60);
var timeS = parseInt(this.times % 3600 % 60 );
this.objTimer.innerHTML = timeH +":"+ timeM + ":" +timeS;
}

}

</script>
<body>
<div id="times_sec">1000</div>

<div id="times_sec2">500</div>

<script>
var aa = new timer ("times_sec");
var cc = aa.init();
var bb = new timer ("times_sec2");
var dd = bb.init();
</script>
</body>
</html>

 

posted on 2010-06-26 17:28  维美启程  阅读(262)  评论(1编辑  收藏  举报