87,922
社区成员
发帖
与我相关
我的任务
分享
String.prototype.toInt=function(){if(/(-?\d+)/.test(this)){return parseInt(RegExp.$1);}else{return 0;}}
//类
var System = {
clock:function(inittime)
{
this.timespan = 0;
var re = /(\d+)h|(\d+)m|(\d+)s/ig;
while(re.exec(inittime))
{
this.timespan += RegExp.$1.toInt()*3600;
this.timespan += RegExp.$2.toInt()*60;
this.timespan += RegExp.$3.toInt();
}
this._ti = null;
}
};
System.clock.prototype = {
//启动方法
run:function(){
var This = this;
this.clock1 = new Date();
this.clock2 = new Date(this.clock1.valueOf() + (this.timespan * 1000));
var tick = function(){
var tl = (This.clock2-new Date());
if((typeof This.ontick)=="function")
This.ontick();
if(tl<0)
{
window.clearInterval(This._ti);
if((typeof This.onend)=="function")This.onend();
tick = null;
}
}
this._ti = window.setInterval(tick,200);
},
//获取剩余毫秒
getTimeLeft:function(){return (a=(this.clock2-new Date()))>=0?a:0;},
//获取经过毫秒
getTimeRun:function(){return (a=(new Date()-this.clock1))>=0?a:0;},
//获取剩余秒
getSecondsLeft:function(){return Math.floor(this.getTimeLeft()/1000);},
//获取经过秒
getSecondsRun:function(){return Math.floor(this.getTimeRun()/1000);},
//获取剩余分钟
getMinutesLeft:function(){return Math.floor(this.getTimeLeft()/60000);},
//获取经过分钟
getMinutesRun:function(){return Math.floor(this.getTimeRun()/60000);},
//获取剩余小时
getHoursLeft:function(){return Math.floor(this.getTimeLeft()/3600000);},
//获取经过小时
getHoursRun:function(){return Math.floor(this.getTimeRun()/3600000);}
}
//调用
//初始化时钟 (参数:时钟时间 1h:1小时 10m:10分 23s:23秒 ["10m23s" "10h80m" "10m" "100s"]都可以)
var aa = new System.clock("1h10m23s");
//绑定 时钟每心跳事件
aa.ontick = function(){
//这里是每次心跳时执行的方法 this.getMinutesLeft() % 60 是取当前剩余分钟数 并且取60的模
document.body.innerHTML =[this.getHoursLeft(),this.getMinutesLeft() % 60,this.getSecondsLeft() % 60].join(":");
}
//绑定 时钟结束事件
aa.onend = function(){
//这里是倒计时完成时 使用的方法
alert("时间到");
}
//开始跑
aa.run();
<!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>无标题文档 </title>
<script type="text/JavaScript">
var stingTime;
var timeLeft = 60 * 60 * 1000;
function countTime()
{
document.getElementById("hehe").disabled=true;
if(timeLeft == 0)
{
alert("时间到!");
return;
}
var startMinutes = parseInt(timeLeft / (60 * 1000), 10);
var startSec = parseInt((timeLeft - startMinutes * 60 * 1000)/1000)
document.getElementById("timeDiv").innerHTML = "剩余时间:" + startMinutes + "分钟" + startSec + "秒";
timeLeft = timeLeft - 1000;
setTimeout('countTime()',1000);
}
</script> </head>
<body>
<input type='button' value='开始考试' onclick='countTime();' id='hehe'>
<div id='timeDiv'></div>
</body> </html>
<script>
function gominutes() {
if (document.main.minutes.value == 0){
document.main.action = "test.jsp";
document.main.submit();
}else{
document.main.minutes.value = eval(document.main.minutes.value + "-1");
window.setTimeout("gominutes()", 1000);//这个是1秒
}
}
</script>
<body onload="gominutes();">
<form name="main">
<table>
<tr>
<td colspan=10 valign=bottom><font color="#ff0000"><b>当前页面每60秒会自动刷新一次:</b></font><input type="text" name="minutes" value="61" style="border=0;color=#4242FF;vertical-align=center;font-weight:bold" readonly size=2></td>
</tr>
</table>
</form>
<!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>无标题文档 </title>
<script type="text/JavaScript">
var lastMinutes=59;
var lastSeconds=60;
var t,stingTime;
function showTime(){
document.getElementById("hehe").disabled=true;
var allTime=60*60*1000;
lastSeconds--;
if(lastSeconds==0 && lastMinutes==0){
clearTimeout(t);
return;
}
else if(lastSeconds<0){
lastSeconds=59;
lastMinutes--;
}
stringTime="还剩时间为:"+lastMinutes+" : "+lastSeconds;
document.getElementById("timeDiv").innerHTML=stringTime;
setTimeout(showTime,1000);
}
</script> </head>
<body>
<input type='button' value='开始考试' onclick='showTime();' id='hehe'>
<div id='timeDiv'></div>
</body> </html>
<script>
var timeLeft = 60 * 60 * 1000;
function countTime()
{
if(timeLeft == 0)
{
alert("时间到!");
return;
}
var startMinutes = parseInt(timeLeft / (60 * 1000), 10);
var startSec = parseInt((timeLeft - startMinutes * 60 * 1000)/1000)
document.body.innerText = "剩余时间:" + startMinutes + "分钟" + startSec + "秒";
timeLeft = timeLeft - 1000;
setTimeout('countTime()',1000);
}
</script>
<body onload="countTime()">
</body>