欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS常用倒计时代码实例总结

程序员文章站 2024-02-01 13:56:22
本文实例总结了js常用倒计时代码。分享给大家供大家参考,具体如下: 第一种:精确到秒的javascript倒计时代码

本文实例总结了js常用倒计时代码。分享给大家供大家参考,具体如下:

第一种:精确到秒的javascript倒计时代码

<form name="form1">
<div align="center" align="center">
<center>离2010年还有:<br>
<input type="textarea" name="left" size="35" style="text-align: center">
</center>
</div>
</form>
<script language="javascript">
startclock()
var timerid = null;
var timerrunning = false;
function showtime() {
today = new date();
var nowhour = today.gethours();
var nowminute = today.getminutes();
var nowmonth = today.getmonth();
var nowdate = today.getdate();
var nowyear = today.getyear();
var nowsecond = today.getseconds();
if (nowyear <2000)
nowyear=1900+nowyear;
today = null;
hourleft = 23 - nowhour
minuteleft = 59 - nowminute
secondleft = 59 - nowsecond
yearleft = 2009 - nowyear
monthleft = 12 - nowmonth - 1
dateleft = 31 - nowdate
if (secondleft<0)
{
secondleft=60+secondleft;
minuteleft=minuteleft-1;
}
if (minuteleft<0)
{
minuteleft=60+minuteleft;
hourleft=hourleft-1;
}
if (hourleft<0)
{
hourleft=24+hourleft;
dateleft=dateleft-1;
}
if (dateleft<0)
{
dateleft=31+dateleft;
monthleft=monthleft-1;
}
if (monthleft<0)
{
monthleft=12+monthleft;
yearleft=yearleft-1;
}
temp=yearleft+'年, '+monthleft+'月, '+dateleft+'天, '+hourleft+'小时, '+minuteleft+'分, '+secondleft+'秒'
document.form1.left.value=temp;
timerid = settimeout("showtime()",1000);
timerrunning = true;
}
var timerid = null;
var timerrunning = false;
function stopclock () {
if(timerrunning)
cleartimeout(timerid);
timerrunning = false;
}
function startclock () {
stopclock();
showtime();
}
// -->
</script>

第二种:某某运动会

<!--倒计时javascript begin-->
<script language="javascript">
<!--
function digitaltime1()
{
var deadline= new date("08/13/2007") //开幕倒计时
var symbol="8月13日"
var now = new date()
var diff = -480 - now.gettimezoneoffset() //是北京时间和当地时间的时间差
var leave = (deadline.gettime() - now.gettime()) + diff*60000
var day = math.floor(leave / (1000 * 60 * 60 * 24))
var hour = math.floor(leave / (1000*3600)) - (day * 24)
var minute = math.floor(leave / (1000*60)) - (day * 24 *60) - (hour * 60)
var second = math.floor(leave / (1000)) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60)
var deadline_2= new date("08/13/2004") //开幕后计时
var symbol_2="8月13日"
var now_2 = new date()
var diff_2 = -480 - now.gettimezoneoffset() //是北京时间和当地时间的时间差
var leave_2 = (now_2.gettime() - deadline_2.gettime()) + diff_2*60000
var day_2 = math.floor(leave_2 / (1000 * 60 * 60 * 24))
var hour_2 = math.floor(leave_2 / (1000*3600)) - (day_2 * 24)
var minute_2 = math.floor(leave_2 / (1000*60)) - (day_2 * 24 *60) - (hour_2 * 60)
var second_2 = math.floor(leave_2 / (1000)) - (day_2 * 24 *60*60) - (hour_2 * 60 * 60) - (minute_2*60)
day=day+1;
day_2=day_2+1;
if (day>0) //还未开幕
{
//liveclock1.innerhtml = "现在"+symbol+"天"
liveclock1.innerhtml = "<font
settimeout("digitaltime1()",1000)
}
if (day<0) //已经开幕
{
//liveclock1.innerhtml = "现在离"+symbol+"还有"+day+"天"+hour+"小时"+minute+"分"+second +"秒"
liveclock1.innerhtml = "<font
settimeout("digitaltime1()",1000)
}
if (day==0) //正在开幕
{
//liveclock1.innerhtml = "现在"+symbol+"天"
liveclock1.innerhtml = "<font
settimeout("digitaltime1()",1000)
}
if (day<0 & day_2>19) //某某运动会结束
{
//liveclock1.innerhtml = "现在离"+symbol+"还有"+day+"天"+hour+"小时"+minute+"分"+second +"秒"
liveclock1.innerhtml = "<font
settimeout("digitaltime1()",1000)
}
}
// -->
</script>
<!--倒计时javascript end-->
<body onload=digitaltime1()>
<div id= liveclock1></div>
</body>

第三种:小时倒计时

<script language="javascript">
<!--
var maxtime = 60*60 //一个小时,按秒计算,自己调整!
function countdown(){
if(maxtime>=0){
minutes = math.floor(maxtime/60);
seconds = math.floor(maxtime%60);
msg = "距离结束还有"+minutes+"分"+seconds+"秒";
document.all["timer"].innerhtml=msg;
if(maxtime == 5*60) alert('注意,还有5分钟!');
--maxtime;
}
else{
clearinterval(timer);
alert("时间到,结束!");
}
}
timer = setinterval("countdown()",1000);
//-->
</script>
<div id="timer" style="color:red"></div>

第四种:最简倒计时

<script language="javascript">
<!-- begin
var timedate= new date("january 14,2006");
var times="研究生考试";
var now = new date();
var date = timedate.gettime() - now.gettime();
var time = math.floor(date / (1000 * 60 * 60 * 24));
if (time >= 0) ;
document.write("<li><font color=#dedbde>现在离2006年"+times+"还有: <font color=#ffffff><b>"+time +"</b></font> 天</font></li>");
// end -->
</script>

第五种:最简倒计时二

<script language="javascript" type="text/javascript">
function djs(){
var urodz= new date("11/12/2008");
var now = new date();
var num
var ile = urodz.gettime() - now.gettime();
var dni = math.floor(ile / (1000 * 60 * 60 * 24));
if (dni >1)
num=dni+1
else if (dni == 1)num=2
else if (dni == 0)num=1
else num=0
document.write(num)
}
</script>
距某某开幕式还有 [<script language="javascript" type="text/javascript">djs()</script>] 天

第五个:javascript倒计时器 - 采用系统时间自校验

这次利用系统时间自校验倒计时, 无需手工调校使得倒计时更为精确, 代码及详细注释如下:

<span id="clock">00:01:11:00</span>
<input id="startb" type="button" value="start countdown!" onclick="run()">
<input id="endb" type="button" value="stop countdown!" onclick="stop()">
<br>
<input id="diff" type="text">
<input id="next" type="text">
<script language="javascript">
var normalelapse = 100;
var nextelapse = normalelapse;
var counter;
var starttime;
var start = clock.innertext;
var finish = "00:00:00:00";
var timer = null;
// 开始运行
function run() {
startb.disabled = true;
endb.disabled = false;
counter = 0;
// 初始化开始时间
starttime = new date().valueof();
// nextelapse是定时时间, 初始时为100毫秒
// 注意setinterval函数: 时间逝去nextelapse(毫秒)后, ontimer才开始执行
timer = window.setinterval("ontimer()", nextelapse);
}
// 停止运行
function stop() {
startb.disabled = false;
endb.disabled = true;
window.cleartimeout(timer);
}
window.onload = function() {
endb.disabled = true;
}
// 倒计时函数
function ontimer()
{
if (start == finish)
{
window.clearinterval(timer);
alert("time is up!");
return;
}
var hms = new string(start).split(":");
var ms = new number(hms[3]);
var s = new number(hms[2]);
var m = new number(hms[1]);
var h = new number(hms[0]);
ms -= 10;
if (ms < 0)
{
ms = 90;
s -= 1;
if (s < 0)
{
  s = 59;
  m -= 1;
}
if (m < 0)
{
  m = 59;
  h -= 1;
}
}
var ms = ms < 10 ? ("0" + ms) : ms;
var ss = s < 10 ? ("0" + s) : s;
var sm = m < 10 ? ("0" + m) : m;
var sh = h < 10 ? ("0" + h) : h;
start = sh + ":" + sm + ":" + ss + ":" + ms;
clock.innertext = start;
// 清除上一次的定时器
window.clearinterval(timer);
// 自校验系统时间得到时间差, 并由此得到下次所启动的新定时器的时间nextelapse
counter++;
var countersecs = counter * 100;
var elapsesecs = new date().valueof() - starttime;
var diffsecs = countersecs - elapsesecs;
nextelapse = normalelapse + diffsecs;
diff.value = countersecs + "-" + elapsesecs + "=" + diffsecs;
next.value = "nextelapse = " + nextelapse;
if (nextelapse < 0) nextelapse = 0;
// 启动新的定时器
timer = window.setinterval("ontimer()", nextelapse);
}
</script>

ps:这里再为大家推荐几款时间及计时器相关工具供大家参考使用:

在线秒表工具:

unix时间戳(timestamp)转换工具:

在线世界各地时间查询:

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript时间与日期操作技巧总结》《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。