<script language=javascript>
var t=3.1415;
alert( "int("+t+") = "+int(t) );
alert( "parseInt("+t+") = "+parseInt(t) );
alert( "Math.floor("+t+") = " +Math.floor(t) );
alert( "Math.round("+t+") = "+Math.round(t) );
alert( "Math.ceil("+t+") = " +Math.ceil(t) );
function int(num){return num-num%1}
</script>
按钮可用状态倒计时...(JavaScript)
今天在一个网站注册的时候, 看到了在服务条款和声明的时候, 我同意这个按钮要等几秒钟才可用, 虽然以前也看到过, 但这几天实在是闲着无聊, 所以, 嘿, 咱也来一个吧.
<form name="frm">
<input type="submit" name="btnSubmit" value="我同意" />
</form>
<script language="javascript">
<!--
var sec = 9;
var wait = sec * 1000;
document.frm.btnSubmit.value = "我同意[" + sec + "]";
document.frm.btnSubmit.disabled = true;
for(var i = 0; i <= sec; i++) {
window.setTimeout("TimeUpdate(" + i + ")", i * 1000);
}
window.setTimeout("TimeOk()", wait);
function TimeUpdate(num) {
if(num != sec) {
var pntNum = (wait / 1000) - num;
document.frm.btnSubmit.value = "我同意[" + pntNum + "]";
}
}
function TimeOk() {
document.frm.btnSubmit.value = "我同意";
document.frm.btnSubmit.disabled = false;
}
//-->
</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">
/* 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: [email protected]
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 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>
----------------------------------------------------
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript动态显示当前时间和倒计时的设计</title>
<script language="javascript" type="text/javascript">
<!--
//获得当前时间,刻度为一千分一秒
var initializationTime=(new Date()).getTime();
function showLeftTime()
{
var now=new Date();
var year=now.getYear();
var month=now.getMonth();
var day=now.getDate();
var hours=now.getHours();
var minutes=now.getMinutes();
var seconds=now.getSeconds();
document.all.show.innerHTML="当前时间:"+year+"年"+month+"月"+day+"日"+hours+"小时"+minutes+"分"+seconds+"秒";
//设定结束时间
//1秒=1000毫秒
endTime=initializationTime+60000;
//设定并显示剩余时间
var leftTime=endTime-(new Date()).getTime();
if(leftTime>0)
{
document.all.showLeft.innerHTML=leftTime+"微秒后停止!";
}
else
{
clearTimeout(timeID);
document.all.showLeft.innerHTML="TimeOut!";
return false;
}
//一秒刷新一次显示时间
var timeID=setTimeout(showLeftTime,1000);
}
//-->
</script>
</head>
<body onload="showLeftTime()">
<label id="show">这里显示开始时间</label><br>
<label id="showLeft">这里显示剩余时间</label>
</body>
</html>
------------------------------------------------
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>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>多个倒计时</title>
<meta name="author" content="Aken [email protected]" />
<script language=javascript>
var array1 = new Array();
var array2 = new Array();
var timerId = new Array();
array1.push(60);
array2.push(0);
array1.push(70);
array2.push(1);
array1.push(50);
array2.push(2);
array1.push(40);
array2.push(3);
array1.push(55);
array2.push(4);
function test(){
for(var i=0;i<array1.length;i++){
timer=setInterval("ee('"+array2[i]+"','"+i+"');",1000);
timerId.push(timer);
}
}
function ee(id,i){
//alert(t+"/"+id);
array1[i]=array1[i]-1;
document.getElementById('input'+id).innerHTML=array1[i];
}
function stop(id){
//array1[id]=100;
clearInterval(timerId[id]);
}
</script>
</head>
<body>
<input type = "button" value = "start" onclick = "test();"/><br/>
<span id="input0">time</span>
<input type = "button" value = "stop" onclick = "stop(0);"/><br/>
<span id="input1">time</span>
<input type = "button" value = "stop" onclick = "stop(1);"/><br/>
<span id="input2">time</span>
<input type = "button" value = "stop" onclick = "stop(2);"/><br/>
<span id="input3">time</span>
<input type = "button" value = "stop" onclick = "stop(3);"/><br/>
<span id="input4">time</span>
<input type = "button" value = "stop" onclick = "stop(4);"/><br/>
</body>