本文实例讲述了JS基于面向对象实现的多个倒计时器功能。分享给大家供大家参考,具体如下:
运行效果图如下:
实现代码如下:
代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript测试文件</title>
</head>
<body>
<div><span id="hour0">0</span>小时</div>
<div><span id="minute0">0</span>分</div>
<div><span id="seconds0">10</span>秒</div>
<br/>
<div><span id="hour1">1</span>小时</div>
<div><span id="minute1">31</span>分</div>
<div><span id="seconds1">31</span>秒</div>
<br/>
<div><span id="hour2">2</span>小时</div>
<div><span id="minute2">32</span>分</div>
<div><span id="seconds2">32</span>秒</div>
<br/>
<div><span id="hour3">3</span>小时</div>
<div><span id="minute3">33</span>分</div>
<div><span id="seconds3">33</span>秒</div>
<br/>
<div><span id="hour4">4</span>小时</div>
<div><span id="minute4">34</span>分</div>
<div><span id="seconds4">34</span>秒</div>
<br/>
</body>
</html>
<script type="text/javascript">
//名山计时器:
function msTimeCount(){
this._hour=0; //“小时”数
this._minute=0; //“分”数
this._seconds=0; //“秒”数
//
this._hourHtmlObj={};//“小时”html对象
this._minuteHtmlObj={};//“分”html对象
this._secondsHtmlObj={};//“秒”html对象
//
this._totalSeconds=0;//总秒数
};
msTimeCount.prototype={
//1.获取对象
$:function(ID){
return document.getElementById(ID);
},
//2.初始化:
/*
* arrTime: 传入时间数组,格式为[1,30,30],代表 1小时30分30秒;
* arrHtmlObj: 更新时间数据的Html对象数组,格式为["hour","minute","seconds"];
*/
init:function(arrTime,arrHtmlObj){
var _this=this;
_this._hour=parseInt(arrTime[0]);
_this._minute=parseInt(arrTime[1]);
_this._seconds=parseInt(arrTime[2]);
_this._hourHtmlObj=_this.$(arrHtmlObj[0]);
_this._minuteHtmlObj=_this.$(arrHtmlObj[1]);
_this._secondsHtmlObj=_this.$(arrHtmlObj[2]);
_this._totalSeconds=parseInt(_this._hour*60*60+_this._minute*60+_this._seconds);
//开始计时:
_this.timeCount();
},
//3.计时器:
timeCount:function(){
var _this=this;
var tmpTimeCount=setInterval(
function(){
_this._totalSeconds--;
//alert(_this._totalSeconds);
_this.refreshTime();
if(_this._totalSeconds<1){
clearInterval(tmpTimeCount);
return;
}
}
,1000);
},
//4.刷新页面时间:
refreshTime:function(){
var _this=this;
if(_this._totalSeconds>0){
_this._hour=parseInt(_this._totalSeconds/3600);
_this._minute=parseInt((_this._totalSeconds-_this._hour*3600)/60);
_this._seconds=_this._totalSeconds-_this._hour*3600-_this._minute*60;
}else{
_this._hour=_this._minute=_this._seconds=0;
}
_this._hourHtmlObj.innerHTML=_this._hour;
_this._minuteHtmlObj.innerHTML=_this._minute;
_this._secondsHtmlObj.innerHTML=_this._seconds;
}
}
var timeCount0=new msTimeCount();
timeCount0.init([0,0,10],["hour0","minute0","seconds0"]);
var timeCount1=new msTimeCount();
timeCount1.init([1,31,31],["hour1","minute1","seconds1"]);
var timeCount2=new msTimeCount();
timeCount2.init([2,32,32],["hour2","minute2","seconds2"]);
var timeCount3=new msTimeCount();
timeCount3.init([3,33,33],["hour3","minute3","seconds3"]);
var timeCount4=new msTimeCount();
timeCount4.init([4,34,34],["hour4","minute4","seconds4"]);
</script>
PS:这里再为大家推荐几款时间及计时器相关工具供大家参考使用:
在线秒表工具:
http://tools.jb51.net/bianmin/miaobiao
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
在线世界各地时间查询:
http://tools.jb51.net/zhuanhuanqi/worldtime
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript时间与日期操作技巧总结》、《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
标签:
JS,面向对象,多个,倒计时器
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“JS基于面向对象实现的多个倒计时器功能示例”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2025年10月25日
2025年10月25日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
