用Javascript制作倒数计秒器

王朝html/css/js·作者佚名  2006-01-30
窄屏简体版  字體: |||超大  

00:00:11:00

新建一个文本文件(后缀名是TXT),然后将下面的源代码全部复制/粘贴到这个文本文件中,保存后改变这个文本文件的后缀名为HTML,然后在IE浏览器中打开并运行。

<span id="clock">00:00:11:00</span>

<input type=button value="start countdown!" onclick="onTimer()">

<input type=button value="stop countdown!" onclick="window.clearTimeout(timer);">

<script language="Javascript">

/* This notice must be untouched at all times.

countdown.js v. 1.0

The latest version is available at

http://blog.csdn.net/yjgx007

Copyright (c) 2004 Xinyi.Chen. All rights reserved.

Created 7/30/2004 by Xinyi.Chen.

Web: http://blog.csdn.net/yjgx007

E-Mail: chenxinyi1978@hotmail.com

Last modified: 7/30/2004

This program is free software;

you can redistribute it and/or modify it under the terms of the

GNU General Public License as published by the Free Software Foundation;

See the GNU General Public License

at http://www.gnu.org/copyleft/gpl.html for more details.

*/

var elapse = 100;

var start = document.all("clock").innerText;

var finish = "00:00:00:00";

var timer = null;

function onTimer(i)

{

if (start == finish)

{

window.clearTimeout(timer);

alert("time is reach!");

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;

document.all("clock").innerText = start;

timer = window.setTimeout("onTimer()",elapse);

}

</script>

考虑到onTimer函数中执行语句的延迟,实际中你可以减少elapse的值,使得倒计数秒更为精确!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航