0

我有这个将显示秒数的 javascript 倒计时。我需要知道如何在柜台显示天数而不是秒数。

即1天,还剩2小时。

这是代码:

    <script type="text/javascript">
var MAX_COUNTER = 1000;
var counter = null;
var counter_interval = null;

function setCookie(name,value,days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length,c.length);
        }
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

function resetCounter() {
    counter = MAX_COUNTER;
}

function stopCounter() {
    window.clearInterval(counter_interval);
    deleteCookie('counter');
}

function updateCounter() {
    var msg = '';
    if (counter > 0) {
        counter -= 1;
        msg = counter;
        setCookie('counter', counter, 1);
    }
    else {
        counter = MAX_COUNTER;

    }
    var el = document.getElementById('counter');
    if (el) {
        el.innerHTML = msg;
    }
}

function startCounter() {
    stopCounter();
    counter_interval = window.setInterval(updateCounter, 1000);
}

function init() {
    counter = getCookie('counter');
    if (!counter) {
        resetCounter();
    }
    startCounter();
}

init();
</script>

目前它只显示秒数,一旦达到 0 就会重新启动。

4

3 回答 3

2
http://jsfiddle.net/h2DEr/1/

function updateCounter() {
    var msg = '';
    if (counter > 0) {
        counter -= 1;
        msg = convertSecondsToDays(counter); 
        setCookie('counter', counter, 1);
    }
    else {
        counter = MAX_COUNTER;
    }
    var el = document.getElementById('counter');
    if (el) {
        el.innerHTML = msg;
    }
}

这是将秒转换为天的函数

function convertSecondsToDays(sec) {
  var days, hours,rem,mins,secs;
  days =  parseInt(sec/(24*3600));
  rem = sec - days*3600
  hours = parseInt(rem/3600);
  rem = rem - hours*3600;
  mins = parseInt(rem/60);
  secs = rem - mins*60;
  return days +" days " + hours +" hours "+mins + " mins "+ secs + " seconds";
}

更新:在@sanya_zol 的回答和大卫史密斯的评论之后

因为 setInterval 不应该每秒运行一次,所以你需要稍微改变你的策略。我也为此修改了小提琴

  1. 当您希望它过期时,将 MAX_COUNTER 设置为一个值。
  2. 不是将计数器减少 -1,而是检查当前时间,从到期日期中减去它并显示它。

      EXPIRY_SECONDS = 24*60*60;
      MAX_COUNTER = parseInt(new Date().getTime()/(1000)) + EXPIRY_SECONDS;
    
      function updateCounter() {
         var msg = '',curTime = parseInt(new Date().getTime()/1000);
    
         if (curTime < MAX_COUNTER) {
            msg = convertSecondsToDays(MAX_COUNTER- curTime);
            setCookie('counter', MAX_COUNTER- curTime, 1);
         }
         else {
            MAX_COUNTER = parseInt(new Date().getTime()/1000) + EXPIRY_SECONDS;
         }
    
         var el = document.getElementById('counter');
         if (el) {
           el.innerHTML = msg
         }
       }
    
于 2013-03-26T11:00:28.957 回答
0
counter_interval = window.setInterval(updateCounter, 1000); 

1000 是以毫秒为单位的值,那么一天有多少毫秒?

counter_interval = window.setInterval(updateCounter, 1000*60*60*24);
于 2013-03-26T10:28:29.463 回答
0

除了vdua的回答:

你的代码写得很糟糕。

它使用 setInterval 计数器不精确(此外,它的精度非常非常差)-因此您的计数器的秒数将等于 1.05-1.2 实际秒数(实时时间和计数器之间的差异将累积)。

您应该每次以较低的间隔(如 100 毫秒)检查系统时间(通过(new Date).getTime() )以获得精确的计数器。

于 2013-03-26T11:03:35.807 回答