js 实现“倒计时” 以及 N秒后跳转页面

时间:2021-09-11 10:14:24
 
    <script type="text/javascript" language="javascript">
        var target=[]
        var time_id=[]
        /*
          原理:每次和当前时间比较,得到天、小时、分、秒
        */
        function show_date_time_0()
        {
            setTimeout("show_date_time_0()", 1000);
            for (var i=0,j=target.length;i<j;i++)
            {
                today=new Date();         
                //计算目标时间与当前时间间隔(毫秒数)
                var timeold=target[i]-today.getTime(); //getTime 方法返回一个整数值,这个整数代表了从 1970 年 1 月 1 日开始计算到 Date 对象中的时间之间的毫秒数。
               
                //计算目标时间与当前时间的秒数
                var sectimeold=timeold/1000;
               
                //计算目标时间与当前时间的秒数(整数)
                var secondsold=Math.floor(sectimeold);
               
                //计算一天的秒数
                var  msPerDay=24*60*60*1000;
              
                //得到剩余天数
                var e_daysold=timeold/msPerDay;
                 //得到剩余天数(整数)
                var daysold=Math.floor(e_daysold);
               
                //得到剩余天数以外的小时数
                var e_hrsold=(e_daysold-daysold)*24;
                 //得到剩余天数以外的小时数(整数)
                var hrsold=Math.floor(e_hrsold);
               
                //得到尾剩余分数
                var e_minsold=(e_hrsold-hrsold)*60;
                //得到尾剩余分数(整数)
                minsold=Math.floor((e_hrsold-hrsold)*60);
               
                //得到尾剩余秒数(整数)
                seconds=Math.floor((e_minsold-minsold)*60);
                if (daysold<0)
                {
                    document.getElementById(time_id[i]).innerHTML="逾期,倒计时已经失效";
                } 
                else
                {
                    //天数取三位,不足时前边补0
                    if (daysold<10) { daysold="0"+daysold }
                    //天数取三位,不足时前边补0
                    if (daysold<100) { daysold="0"+daysold }
                   
                    //小时取两位,不足补0
                    if (hrsold<10) { hrsold="0"+hrsold }
                    //分数取两位,不足补0
                    if (minsold<10) {minsold="0"+minsold}
                    //秒数取两位,不足补0
                    if (seconds<10) {seconds="0"+seconds}
                   
                    //小于三天时,字体为红色
                    if (daysold<3) {
                        document.getElementById(time_id[i]).innerHTML="<font color=red>"+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒</font>";
                    } 
                    else
                    {
                        document.getElementById(time_id[i]).innerHTML=daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";
                    }
                }
                
            }
        }
        setTimeout("show_date_time_0()", 100);
    </script>
    时间1   2008-08-08 04:00:00:
    <div id="TimeCounter_0" style="border: 1px solid black; margin: 5px; padding: 2px;
        width: 300px">
    </div>
    <script type="text/javascript">
        //设定目标时间(数组元素)
        //target[target.length]=new Date(年,月-1,日,时,分,秒).getTime()
        target[target.length]=new Date(2008,08-8,04,00,00,00).getTime();
       
        //设定倒计时显示地址(数组元素)
        time_id[time_id.length]="TimeCounter_0"
    </script>
   时间2   2020-08-08 08:08:08:
    <div id="TimeCounter_1" style="border: 1px solid black; margin: 5px; padding: 2px;
        width: 300px">
    </div>
    <script type="text/javascript">
        target[target.length]=new Date(2020,8,8,8,8,8).getTime()
        time_id[time_id.length]="TimeCounter_1"
    </script>
    时间3   2099-08-08 08:08:08:
    <div id="TimeCounter_3" style="border: 1px solid black; margin: 5px; padding: 2px;
        width: 300px">
    </div>
    <script type="text/javascript">
        target[target.length]=new Date(2099,8,8,8,8,0).getTime()
        time_id[time_id.length]="TimeCounter_3"
    </script>
  <br><br><br><br>
<span id="mytime" style="color:#FF0000;">10000</span> 秒钟自动跳转到登陆页
<script>
function runtime()
{
 document.getElementById('mytime').innerHTML=document.getElementById('mytime').innerHTML-1;
 if(document.getElementById('mytime').innerHTML==0)
 {
  location.href='Login.aspx';
 }
 setTimeout("runtime()",1000);
}
runtime();
</script>