前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Javascript倒计时

Javascript倒计时

作者头像
meteoric
发布2019-02-25 16:48:19
9230
发布2019-02-25 16:48:19
举报
文章被收录于专栏:游戏杂谈游戏杂谈
代码语言:javascript
复制
<!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>
  <title> new document </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <meta http-equiv="content-type" content="text/htm;charset=utf-8" />
 </head>

 <body>
  <div>
	距离广州亚运会开幕时间2010年11月12还有:<span id="otherTime"></span>
  </div>
<script type="text/javascript">
	var objElem = document.getElementById("otherTime");

	var endTime = new Date('2010', '10', '12').getTime();

	var nowTime = null;
	var laveTimer = null;

	function laveTime() {
		nowTime = new Date().getTime();

		var a = endTime - nowTime;//距离指定的时间还有多少毫秒..

		if (a > 0) {
			
			var arr_1 = [1000*60*60*24, 1000*60*60, 1000*60, 1000],
				arr_2 = ['天', '小时', '分', '秒'],
				temp = null;
			
			var timeDesArr = [];

			for (var i = 0, len = arr_1.length; i<len ; i++) {
				temp = Math.floor(a/arr_1[i]);

				if (temp > 0) {
					timeDesArr.push((String(temp).length == 1 ? ("00"+temp).substr(1) : temp) + arr_2[i]);
				}
				
				a -= arr_1[i]*temp;
			}

			objElem.innerHTML = timeDesArr.join("");

		} else {
			objElem.innerHTML = "到时间了!";
			clearInterval(laveTime);
			laveTime = null;
		}
	}
	
	//因为我们的时候是一秒一秒的在走,那么也需要一秒一秒的改变一下这个剩余时间了,一秒执行一次laveTime函数
	laveTimer = setInterval(laveTime, 1*1000);
</script>

 </body>
</html>
代码语言:javascript
复制
代码语言:javascript
复制

运行示例代码: <!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> <title> new document </title> </head> <body> <div> 距离广州亚运会开幕时间2010年11月12还有:<span id="otherTime"></span> </div> </body> </html> 运行

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-09-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档