110,539
社区成员
发帖
与我相关
我的任务
分享
// 计时器类
function CountDown()
{
this.customDateTime=({years:0,months:0,days:0,hours:0,minutes:0,seconds:0,Ms:0});
this.timerVariable=1111;
this.countMs=0;
this.successCallBack="";
this.triggerCallBack="";
this.IsRun=false;
}
CountDown.prototype =
{
//计时开始并返回自定义时间对象,countMs倒计时的毫秒数,callBack回调函数名称
timer:function(countMs,successCallBack,triggerCallBack)
{
/* alert("sss");
alert(this.timer);
alert(this.timerVariable+"\n"+triggerCallBack+"()\n"+successCallBack+"\n"+triggerCallBack);*/
this.successCallBack=successCallBack;
this.triggerCallBack=triggerCallBack;
this.countMs=countMs;
this.IsRun=true;
var THIS=this;
this.countMs=this.countMs-1000;
this.convertToDateTime(this.countMs);
eval(this.triggerCallBack+"()");
if(this.countMs<=0)
{
this.IsRun=false;
alert(this.successCallBack);
eval(this.successCallBack+"()");
return;
}
function run()
{
THIS.timer(THIS.countMs,THIS.successCallBack,THIS.triggerCallBack);
}
if(this.IsRun==true)
{
setTimeout(run(),10000);
}
},
//将时分秒转化为毫秒type:Hours/Minutes/Seconds
convertToMS:function(count,type)
{
var customMs=0;
if(count>0)
{
if(Type=="Hours")
{
customMs=count*60*60*1000;
}
else if(Type=="Minutes")
{
customMs=count*60*1000;
}
else if(Type=="Seconds")
{
customMs=count*1000;
}
}
},
//将毫秒转化为自定义时间对象输出
convertToDateTime:function(inputMs)
{
var years=0;
var months=0;
var days=0;
var hours=0;
var minutes=0;
var seconds=0;
var Ms=0;
if(inputMs>0)
{
if(inputMs>=(365*24*60*60*1000))
{
years=Math.floor(inputMs/(365*24*60*60*1000));
inputMs=inputMs-(365*24*60*60*1000)*years;
}
if(inputMs>=(30*24*60*60*1000))
{
months=Math.floor(inputMs/(30*24*60*60*1000));
inputMs=inputMs-months*(30*24*60*60*1000);
}
if(inputMs>=(24*60*60*1000))
{
days=Math.floor(inputMs/(24*60*60*1000));
inputMs=inputMs-days*(24*60*60*1000);
}
if(inputMs>=(60*60*1000))
{
hours=Math.floor(inputMs/(60*60*1000));
inputMs=inputMs-hours*(60*60*1000);
}
if(inputMs>=(60*1000))
{
minutes=Math.floor(inputMs/(60*1000));
inputMs=inputMs-minutes*(60*1000);
}
if(inputMs>=1000)
{
seconds=Math.floor(inputMs/1000);
inputMs=inputMs-seconds*1000;
}
if(inputMs>1)
{
Ms=inputMs;
}
this.customDateTime.years=years;
this.customDateTime.months=months;
this.customDateTime.days=days;
this.customDateTime.hours=hours;
this.customDateTime.minutes=minutes;
this.customDateTime.seconds=seconds;
this.customDateTime.Ms=Ms;
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript" src="../JS/Timer.js"></script>
<script type="text/javascript" >
var Timer=new CountDown();
var inputMs=1000*60;
Timer.timer(inputMs,'Success','Trigger');
function Trigger()
{
var MyDate=Timer.customDateTime;
var Result="";
Result+="Years:"+MyDate.years+"<br />";
Result+="Months:"+MyDate.months+"<br />";
Result+="Days:"+MyDate.days+"<br />";
Result+="Hours:"+MyDate.hours+"<br />";
Result+="Minutes"+MyDate.minutes+"<br />";
Result+="Seconds:"+MyDate.seconds+"<br />";
Result+="Ms:"+MyDate.Ms+"<br />";
document.write(Result);
}
function Success()
{
alert("Success");
return;
}
</script>
</body>
</html>