倒计时以服务器时间计算,下面这段代码有问题。麻烦高手帮忙看看startclock()   
var timerID = null;   
var timerRunning = false;   
function showtime() {
try{
var http = new XMLHttpRequest;   
http.open("HEAD", ".", false);   
http.send(null);
var now = new Date(http.getResponseHeader("Date"))
}catch(e){var now = new Date()}
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds()
//var days = parseInt(secondes/(24*3600*1000));
//var nowt = new Date(year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second);
//var destination_t = new Date("2013/5/26 23:59:59")
var Today = null;   
var days = parseInt(secondes/(24*3600*1000));
var nowt = new Date(year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second);
var destination_t = new Date("2013/09/22 23:59:59")

var secondes = destination_t.getTime()-nowt.getTime();
var days = secondes>1? parseInt(secondes/(24*3600*1000)):0;
var hours = secondes>1? parseInt((secondes-(days*24*3600*1000))/3600000):0;
var minus = secondes>1? parseInt((secondes-(days*24*3600*1000)-(hours*3600000))/60000):0;
var secondes = secondes>1? parseInt((secondes-(days*24*3600*1000)-(hours*3600000)-(minus*60000))/1000):0;$(".time_show dd").eq(0).text(days);
$(".time_show dd").eq(1).text(hours);
$(".time_show dd").eq(2).text(minus);
$(".time_show dd").eq(3).text(secondes);
timerID = setTimeout("showtime()",1000);   
timerRunning = true;   
}   
var timerID = null;   
var timerRunning = false;   
function stopclock () {   
if(timerRunning)   
clearTimeout(timerID);   
timerRunning = false;   
}   
function startclock () {   
stopclock();   
showtime();   
倒计时

解决方案 »

  1.   

    LZ有两个地方需要改动var timerID = null;
    var timerRunning = false;
    function showtime() {
        var now;
        try {
            var http = new XMLHttpRequest;
            http.open("HEAD", ".", false);
            http.send(null);
            var now = new Date(http.getResponseHeader("Date"))
        } catch(e) {
            now = new Date()
        }
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var day = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds()
        //var days = parseInt(secondes/(24*3600*1000));
        //var nowt = new Date(year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second);
        //var destination_t = new Date("2013/5/26 23:59:59")
        var Today = null;
        var days = parseInt(secondes / (24 * 3600 * 1000));
        var nowt = new Date(year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second);
        var destination_t = new Date("2013/09/22 23:59:59")    var secondes = destination_t.getTime() - nowt.getTime();
        var days = secondes > 1 ? parseInt(secondes / (24 * 3600 * 1000)) : 0;
        var hours = secondes > 1 ? parseInt((secondes - (days * 24 * 3600 * 1000)) / 3600000) : 0;
        var minus = secondes > 1 ? parseInt((secondes - (days * 24 * 3600 * 1000) - (hours * 3600000)) / 60000) : 0;
        var secondes = secondes > 1 ? parseInt((secondes - (days * 24 * 3600 * 1000) - (hours * 3600000) - (minus * 60000)) / 1000) : 0;    $(".time_show dd").eq(0).text(days);
        $(".time_show dd").eq(1).text(hours);
        $(".time_show dd").eq(2).text(minus);
        $(".time_show dd").eq(3).text(secondes);
        timerID = setTimeout("showtime()", 1000);
        timerRunning = true;
    }
    var timerID = null;
    var timerRunning = false;
    function stopclock() {
        if (timerRunning) clearTimeout(timerID);
        timerRunning = false;
    }
    function startclock() {
        stopclock();
        showtime();
    }
    startclock();
      

  2.   

    直接 两date对象相减,再创建date<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>  
     <style>
        dd{list-style:none;display:inline}
     </style>
    <div class="time_show">
    <dd></dd>日 <dd></dd> 时 <dd></dd>分<dd></dd>杪
    </div>
    <script>
    startclock();
    var timerID=null;
    var timerRunning=false;
    var destination="2013/09/22 23:59:59";
    function showtime(){
      try{
        var http=new XMLHttpRequest;
        http.open("HEAD","?",false);
        http.send(null);
        var now=new Date(http.getResponseHeader("Date"));
      }
      catch(e){
        var now=new Date();
      }
       
      var destination_t=new Date(destination);
     
      var _d=new Date((destination_t -now )|| 0 );
     
      
      $(".time_show dd").eq(0).text(  _d.getDate());
      $(".time_show dd").eq(1).text( _d.getHours());
      $(".time_show dd").eq(2).text(_d.getMinutes()  );
      $(".time_show dd").eq(3).text(_d.getSeconds());
      timerID=setTimeout("showtime()",1000);
      timerRunning=true;
    }
    var timerID=null;
    var timerRunning=false;
    function stopclock(){
      if(timerRunning){
        clearTimeout(timerID);
      }
      timerRunning=false;
    }
    function startclock(){
      stopclock();
      showtime();
    }

    </script>