向大牛问一个关于JS的问题,

qin442799037 2009-05-11 05:17:59
一个计时器类
// 计时器类 
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>

一个计时器类

JScript code

<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>


HTML code

<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>


我这样写是有错误的,我找不到错误在哪里,大侠们可以拷过去测试一下
问题:
1:为什么我的setTimeout无返回值
2:我该怎么样实现这个计时器
...全文
114 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝海D鱼 2009-05-11
  • 打赏
  • 举报
回复
up

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧