// JScript 文件
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="";}
CountDown.prototype =
{
//计时开始并返回自定义时间对象,countMs倒计时的毫秒数,successCallBack回调函数名称,触发回调triggerCallBack
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;
   eval(successCallBack+"()");
}
function run()
{
   THIS.timer(THIS.countMs,THIS.successCallBack,THIS.triggerCallBack);
}
if(this.IsRun)
{
                  //setTimeout无返回值
   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;
    }
    
   
}
  
}

解决方案 »

  1.   

    问题:
    1:为什么我的setTimeout无返回值
    2:我该怎么样实现这个计时器
      

  2.   

    页面用法<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>
      

  3.   

    div   id=test   ></div>   
      <script>   
      var   alltime=300;   
      function   setTime(){   
      if   (alltime<=0){   
      alert("时间到!");   
      clearInterval(s);   
      }   
      else{   
      alltime--;   
      m=Math.floor(alltime/60);   
      se=Math.round(alltime-(Math.floor(alltime/60)*60));   
      test.innerText="剩余"+m+"分"+se+"秒";   
      }   
      }   
      s=setInterval("setTime()",1000)   
      </script>  1.计时器的范例代码
    <html>
    <head>
    <title>计时器的范例</title>
    <script language="javascript">
    <!--
    var CallTimeLen = "0";
    var timer1 = null;function DoCallTimer()
    {  
      var minute="0";
         var second="0";
      CallTimeLen = parseInt(CallTimeLen)+1;
      minute = parseInt(CallTimeLen/60);
      second = CallTimeLen%60;
      if(minute=="0")
      {
       document.frmtimer.thzt.innerText ="您已通话:"+second+"秒";
      }
      else
      {
       document.frmtimer.thzt.innerText ="您已通话:"+minute+"分"+second+"秒";
      }
      window.timer1 = window.setTimeout("DoCallTimer()",1000);
    }
    function stop()
    {
     clearTimeout(window.timer1);
    }
    -->
    </script>
    </head>
    <body>
    <form name="frmtimer" action="" method="post">
    <hr>
    <input type="text" name="thzt" id="thzt">
    <br>
    <input type="button" value="开始计时" name="start" onClick="DoCallTimer()">
    <input type="button" value="停止" name="stop" onClick="clearTimeout(window.timer1);">
    <hr>
    </form>
    </body>
    </html>  2.倒计时器的范例代码
    <html>
    <head>
    <title>计时器的范例</title>
    <script language="javascript">
    <!--
    var CallTimeLen = "0";
    var begintime;
    var timer = null;function DoConverseCallTimer()
    {
     //alert(document.frmtimer.thzt.value);
     if(document.frmtimer.thzt.value == "")
      alert("请输入倒计时开始的秒数!");
     else
     {
      document.frmtimer.conversestart.disabled=true;
      var minute="0";
         var second="0";
      begintime = parseInt(begintime)-1;
      minute = parseInt(begintime/60);
      second = begintime%60;
      if(minute=="0")
      {
       document.frmtimer.thzt.innerText ="您剩余的时间为:"+second+"秒";
      }
      else
      {
       document.frmtimer.thzt.innerText ="您剩余的时间为:"+minute+"分"+second+"秒";
      }
      timer1 = window.setTimeout("DoConverseCallTimer()",1000);
     }
    }function setBegintime()
    {
     document.frmtimer.conversestart.disabled=false;
     begintime = document.frmtimer.thzt.value;
    }
    -->
    </script>
    </head>
    <body>
    <form name="frmtimer" action="" method="post">
    <hr>
    <input type="text" name="thzt" id="thzt" onKeyup="value="/blog/value.replace(/["^\d]/g,'');setBegintime()">
    <br>
    <input type="button" value="开始倒计时" name="conversestart" onClick="DoConverseCallTimer()">
    <input type="button" value="停止" name="stop" onClick="clearTimeout(window.timer1);">
    <hr>
    </form>
    </body>
    </html>
      

  4.   

    改成这样应该就可以了,前提是你的程序能运行到这里。window.setTimeout("run()",10000);
      

  5.   

    window.setTimeout
    我用过不行
    还是没返回值