用js代码在页面显示倒计时的方法

不废话直接上代码

 1 <script language="javascript">
 2                     //倒计时
 3                     var interval = 1000;
 4                     var nowserver = new Date('@DateTime.Now.ToString()');//服务器时间
 5                     var now = new Date();//客户端时间
 6                     var datediff = nowserver.getTime() - now.getTime();//server-client时间差
 7                     //alert(datediff);
 8 
 9                     var endDate = new Date('@DateTime.Now.AddMinutes(10).ToString()');//不动 注意这里@DateTime.Now.AddMinutes(10).ToString()是.NET代码
10 
11                     //重新发送的方法
12     //function payverify() {
13     //    $.post('/RetailSales/PayVerify', { orderno: "@Model.OrderNumber", orderid: "@Model.OrdersId", paypwd: $('#paypwd').val() }, function (data) {
14      //       $('#buttompay').html(data);
15 //
16      //   });
17    // }
18 
19                     function ShowCountDown(divname) {
20                         now = new Date();//客户端时间,是反复要变的
21                         var leftTime = endDate.getTime() - now.getTime() + datediff;//修复时间差
22                         var cc = document.getElementById(divname);
23                         if (leftTime > 0) {
24                             var leftsecond = parseInt(leftTime / 1000);
25                             var day1 = Math.floor(leftsecond / (60 * 60 * 24));
26                             var hour = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
27                             var minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour * 3600) / 60);
28                             var second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour * 3600 - minute * 60);
29                             try {
30                                 var cc = document.getElementById(divname);
31                                 if (minute == 0 && second == 0) {
32                                     cc.innerHTML = "0分0秒";
33                                     document.getElementById('msg').innerHTML = "信息:<font color=red>短信验证超时,请重新获取短信验证码!<span>";
34                                     window.clearInterval(intervalId);//停止计时
35                                     return;
36                                 }
37                                 cc.innerHTML = minute + "分" + second + "秒";
38                             } catch (e) { }
39                         }
40                         else {
41                             cc.innerHTML = "0分0秒";
42                             document.getElementById('msg').innerHTML = "信息:<font color=red>短信验证超时,请重新获取短信验证码!<span>";
43                             window.clearInterval(intervalId);//停止计时
44                         }
45                     }
46                     var intervalId = window.setInterval(function () { ShowCountDown('t'); }, interval);
47                 </script>

 

posted on 2015-02-13 11:15  波澜不惊super  阅读(1130)  评论(0编辑  收藏  举报

导航