每次到最后2秒的ajax就开始请求 然后倒计时就会卡住 照理说我设置的10秒请求一次 而计时器是1秒1秒的倒计时 为什么会走了8秒就开始请求了呢?因为我需要每10秒同步下服务器时间。 请大神帮忙看看问题在哪 怎么修改
<!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>Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
用定时器之前先清除定时器
调用的时候 先清除一下 上次的数据