Every time the ajax reaches the last 2 seconds, it will start requesting and then the countdown will get stuck. Logically speaking, I set the request once every 10 seconds and the timer is 1 second. Why does the countdown start after 8 seconds? ? Because I need to synchronize the server time every 10 seconds. Please help me find out where the problem is and how to modify it
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/jquery.min.js"></script> <style type="text/css"> </style> <script type="text/javascript"> time = null function timedCount() { t = 10; $.ajax({ type : 'get', url : 'ssc_cq.php', dataType : 'json', success : function(a){ // var t = a.time.substr(a.time.length-2); // var m = a.time.charAt(a.time.length -4); // var e = parseInt(m*60) + parseInt(t); // time = parseInt(600) - parseInt(e); var intDiff = parseInt(t); time = timer(intDiff); } }); //在重新开启一个计时器之前关闭已经开启的计时器 setTimeout("timedCount()",10000); if (time) { clearInterval(time); } } //倒计时总秒数量 function timer(intDiff) { var time = window.setInterval( function () { var day = 0, hour = 0, minute = 0, second = 0; //时间默认值 if (intDiff > 0) { day = Math.floor(intDiff / (60 * 60 * 24)); hour = Math.floor(intDiff / (60 * 60)) - (day * 24); minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60); second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60); } if (minute <= 9) minute = '0' + minute; if (second <= 9) second = '0' + second; $('#day_show').html(day + "天"); $('#hour_show').html('<s id="h"></s>' + hour + '时'); $('#minute_show').html('<s></s>' + minute + '分'); $('#second_show').html('<s></s>' + second + '秒'); intDiff--; if(minute == 0 && second == 0){ time2(3) if(time2 == 0){ location.reload(); } } },1000); return time; } $(function () { timedCount() }); function time2(sj){ var t = window.setInterval( function(){ var m = 0, s = 0; if (sj > 0){ m = Math.floor(sj / 60); s = Math.floor(sj) - (m * 60); } if (m <= 9) m = '0' + m; if (s <= 9) s = '0' + s; $('#minute_show1').html('<s></s>' + m + '分'); $('#second_show1').html('<s></s>' + s + '秒'); sj--; },1000); return t; } </script> </head> <body> <span id="day_show"></span> <span id="hour_show"></span> <span id="minute_show"></span> <span id="second_show"></span> <br> <span id="minute_show1"></span> <span id="second_show1"></span> </body> </html>