初学javascript,要求编写一个倒计时,格式如00:00:00该怎么实现?时间是自己设定,想设置多少秒就多少秒 5
2个回答
2012-03-10 · 知道合伙人软件行家
关注
展开全部
我也是业余爱好者, 以下试编写脚本如下:
<html>
<head>
<title>倒计时测试</title>
<script language="JavaScript">
var DifferenceHour = -1;
var DifferenceMinute = -1;
var DifferenceSecond = -1;
var Tday = new Date(2012,2,10,22,0,0,0); //倒计时时间点-年,月,日,时,分,秒,毫秒
var DAY_MS = 24 * 60 * 60 * 1000;
var HOUR_MS = 60 * 60 * 1000;
var SECOND_MS = 60 * 1000;
var MS = 1000;
function formatstr(value)
{
return (value<9)?"0"+value:value;
}
function clock()
{
var time = new Date();
if(Tday.getTime()<time.getTime())
{
alert("当前倒计时已终止!");
return;
};
var nowtime = time.toLocaleString();
var totime = Tday.toLocaleString();
var Diffms = Tday.getTime() - time.getTime(); //毫秒差
DifferenceHour = Math.floor(Diffms / DAY_MS); //转换成小时
Diffms -= DifferenceHour * DAY_MS;
DifferenceMinute = Math.floor(Diffms / HOUR_MS); //转换成分钟
Diffms -= DifferenceMinute * HOUR_MS;
DifferenceSecond = Math.floor(Diffms / SECOND_MS); //转换成秒
Diffms -= DifferenceSecond * SECOND_MS;
var dSecs = Math.floor(Diffms / MS); //转换成毫秒
var havetime = formatstr(DifferenceMinute)+" : "+formatstr(DifferenceSecond)+" : "+formatstr(dSecs);
document.formnow.totime.value = totime;
document.formnow.nowtime.value = nowtime;
document.formnow.havetime.value = havetime;
setTimeout("clock()",1000);
}
</script>
</head>
<!--BODY里面的ONLOAD注意-->
<body onload="clock();return true" text="red">
<!--实现显示-->
<form name="formnow">
<!--input name="dd" type="text" style="border:0;" size=2>
天-->
设定时间:
<input name="totime" type="text" style="border:0;" size=25 />
<br />
当前时间:
<input name="nowtime" type="text" style="border:0;" size=25 />
<br /><br />
倒计时时间:
<input name="havetime" type="text" style="border:0;" size=25 />
<br />
</form>
<html>
<head>
<title>倒计时测试</title>
<script language="JavaScript">
var DifferenceHour = -1;
var DifferenceMinute = -1;
var DifferenceSecond = -1;
var Tday = new Date(2012,2,10,22,0,0,0); //倒计时时间点-年,月,日,时,分,秒,毫秒
var DAY_MS = 24 * 60 * 60 * 1000;
var HOUR_MS = 60 * 60 * 1000;
var SECOND_MS = 60 * 1000;
var MS = 1000;
function formatstr(value)
{
return (value<9)?"0"+value:value;
}
function clock()
{
var time = new Date();
if(Tday.getTime()<time.getTime())
{
alert("当前倒计时已终止!");
return;
};
var nowtime = time.toLocaleString();
var totime = Tday.toLocaleString();
var Diffms = Tday.getTime() - time.getTime(); //毫秒差
DifferenceHour = Math.floor(Diffms / DAY_MS); //转换成小时
Diffms -= DifferenceHour * DAY_MS;
DifferenceMinute = Math.floor(Diffms / HOUR_MS); //转换成分钟
Diffms -= DifferenceMinute * HOUR_MS;
DifferenceSecond = Math.floor(Diffms / SECOND_MS); //转换成秒
Diffms -= DifferenceSecond * SECOND_MS;
var dSecs = Math.floor(Diffms / MS); //转换成毫秒
var havetime = formatstr(DifferenceMinute)+" : "+formatstr(DifferenceSecond)+" : "+formatstr(dSecs);
document.formnow.totime.value = totime;
document.formnow.nowtime.value = nowtime;
document.formnow.havetime.value = havetime;
setTimeout("clock()",1000);
}
</script>
</head>
<!--BODY里面的ONLOAD注意-->
<body onload="clock();return true" text="red">
<!--实现显示-->
<form name="formnow">
<!--input name="dd" type="text" style="border:0;" size=2>
天-->
设定时间:
<input name="totime" type="text" style="border:0;" size=25 />
<br />
当前时间:
<input name="nowtime" type="text" style="border:0;" size=25 />
<br /><br />
倒计时时间:
<input name="havetime" type="text" style="border:0;" size=25 />
<br />
</form>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询