首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >倒计时器-如何避免重设定时器

倒计时器-如何避免重设定时器
EN

Stack Overflow用户
提问于 2014-05-12 18:21:18
回答 2查看 141关注 0票数 0

我必须添加一个倒计时计时器在我的在线测试网站,但每次我点击下一个或任何其他按钮,计时器重置。我该怎么做才能避免这种情况?这是我使用的代码

代码语言:javascript
复制
   <span id="countdown-1">10 seconds</span>
   <script type="text/javascript">
   // Initialize clock countdowns by using the total seconds in the elements tag
   secs       = parseInt(document.getElementById('countdown-1').innerHTML,10);
   setTimeout("countdown('countdown-1',"+secs+")", 1000);
   secs       = parseInt(document.getElementById('countdown-2').innerHTML,10);
   setTimeout("countdown('countdown-2',"+secs+")", 1000);

    function countdown(id, timer){
    timer--;
    minRemain  = Math.floor(timer / 60);
    secsRemain = new String(timer - (minRemain * 60));
    // Pad the string with leading 0 if less than 2 chars long
    if (secsRemain.length < 2) {
        secsRemain = '0' + secsRemain;
    }

    // String format the remaining time
    clock      = minRemain + ":" + secsRemain;
    document.getElementById(id).innerHTML = clock;
    if ( timer > 0 ) {
        // Time still remains, call this function again in 1 sec
        setTimeout("countdown('" + id + "'," + timer + ")", 1000);
    } else {
        // Time is out! Hide the countdown

     alert('time finished');
     return false;
        document.getElementById(id).style.display = 'none';
    }
}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-12 18:30:30

假设你知道你要数到什么日期。

代码语言:javascript
复制
<?php
$dueTime = mktime(0,0,0, 1, 1, 2015);
$secondsRemaining = $dueTIme - time();
?>

<span id="countdown-1"><?php echo $secondsRemaining; ?> seconds</span>
       <script type="text/javascript">
       // Initialize clock countdowns by using the total seconds in the elements tag
       secs       = parseInt(document.getElementById('countdown-1').innerHTML,10);
       setTimeout("countdown('countdown-1',"+secs+")", 1000);
       secs       = parseInt(document.getElementById('countdown-2').innerHTML,10);
       setTimeout("countdown('countdown-2',"+secs+")", 1000);

        function countdown(id, timer){
        timer--;
        minRemain  = Math.floor(timer / 60);
        secsRemain = new String(timer - (minRemain * 60));
        // Pad the string with leading 0 if less than 2 chars long
        if (secsRemain.length < 2) {
            secsRemain = '0' + secsRemain;
        }

        // String format the remaining time
        clock      = minRemain + ":" + secsRemain;
        document.getElementById(id).innerHTML = clock;
        if ( timer > 0 ) {
            // Time still remains, call this function again in 1 sec
            setTimeout("countdown('" + id + "'," + timer + ")", 1000);
        } else {
            // Time is out! Hide the countdown

         alert('time finished');
         return false;
            document.getElementById(id).style.display = 'none';
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2014-05-12 18:32:02

因为Javascript是客户端,所以每次刷新页面时它都会刷新倒计时。

您必须对给定的timestamp进行倒计时。由于这一点,你将能够保持正确的计数。

有些事情是这样的:(函数来自here)

代码语言:javascript
复制
function getTimestamp(str) {
  var d = str.match(/\d+/g); // extract date parts
  return +new Date(d[0], d[1] - 1, d[2], d[3], d[4], d[5]); // build Date object
}

var countdown = getTimestamp("2012-05-12 18:00:00") - (+new Date);

因为时间戳是未来的一个修正点!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23616093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档