简体   繁体   English

设定倒数计时器的日期

[英]Set date for countdown timer

I need to set countdown timer on my website. 我需要在我的网站上设置倒数计时器。 I have a deadline date and now the timer needs to countdown till that date. 我有一个截止日期,现在计时器需要倒计时到那个日期。

My current code is: 我当前的代码是:

  <script>
                    function getTimeRemaining(endtime) {
              var t = Date.parse(endtime) - Date.parse(new Date());
              var seconds = Math.floor((t / 1000) % 60);
              var minutes = Math.floor((t / 1000 / 60) % 60);
              var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
              var days = Math.floor(t / (1000 * 60 * 60 * 24));
              return {
                'total': t,
                'days': days,
                'hours': hours,
                'minutes': minutes,
                'seconds': seconds
              };
            }

            function initializeClock(id, endtime) {
              var clock = document.getElementById(id);
              var daysSpan = clock.querySelector('.days');
              var hoursSpan = clock.querySelector('.hours');
              var minutesSpan = clock.querySelector('.minutes');
              var secondsSpan = clock.querySelector('.seconds');

              function updateClock() {
                var t = getTimeRemaining(endtime);

                daysSpan.innerHTML = t.days;
                hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
                minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
                secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);

                if (t.total <= 0) {
                  clearInterval(timeinterval);
                }
              }

              updateClock();
              var timeinterval = setInterval(updateClock, 1000);
            }

            var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
            initializeClock('clockdiv', deadline);
        </script>

With this code, every time I visit my website, the timer starts to countdown from "15days". 使用此代码,每次我访问我的网站时,计时器都会从“ 15天”开始倒计时。

Sometimes it's necessary to preserve the state of the clock for more than just the current page. 有时,有必要将时钟状态保留的时间不仅限于当前页面。 For example, if we wanted a ten minute countdown across the site, we wouldn't want the clock to reset every time the user goes to a different page or every time the user refreshes the page they are on. 例如,如果我们想在整个网站上倒数10分钟,那么我们不希望每次用户转到另一个页面时或每次用户刷新其所在页面时都重新设置时钟。

One solution is to save the clock's end time in a cookie. 一种解决方案是将时钟的结束时间保存在cookie中。 That way, navigating to a new page won't reset the end time to ten minutes from now. 这样,导航到新页面不会将结束时间重置为现在的十分钟。

Here's the logic: 这是逻辑:

  1. If a deadline was recorded in a cookie, use that deadline. 如果Cookie中记录了截止日期,请使用该截止日期。
  2. If the cookie isn't present, set a new deadline and store it in a cookie. 如果不存在该cookie,则设置一个新的截止日期并将其存储在cookie中。

To implement this, replace the deadline variable with the following: 要实现这一点,请使用以下命令替换deadline变量:

// if there's a cookie with the name myClock, use that value as the deadline
if(document.cookie && document.cookie.match('myClock')){
  // get deadline value from cookie
  var deadline = document.cookie.match(/(^|;)myClock=([^;]+)/)[2];
}

// otherwise, set a deadline 10 minutes from now and 
// save it in a cookie with that name
else{
  // create deadline 10 minutes from now
  var timeInMinutes = 10;
  var currentTime = Date.parse(new Date());
  var deadline = new Date(currentTime + timeInMinutes*60*1000);

  // store deadline in cookie for future reference
  document.cookie = 'myClock=' + deadline + '; path=/; domain=.yourdomain.com';
}

EDITED: 编辑:

Also you can remove your code 您也可以删除代码

var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);

And replace with this line. 并替换为此行。 You will create date for countdown 您将创建倒计时日期

var deadline = 'December 31 2018 23:59:59 GMT+0200';

This is full code: 这是完整的代码:

 function getTimeRemaining(endtime) { var t = Date.parse(endtime) - Date.parse(new Date()); var seconds = Math.floor((t / 1000) % 60); var minutes = Math.floor((t / 1000 / 60) % 60); var hours = Math.floor((t / (1000 * 60 * 60)) % 24); var days = Math.floor(t / (1000 * 60 * 60 * 24)); return { 'total': t, 'days': days, 'hours': hours, 'minutes': minutes, 'seconds': seconds }; } function initializeClock(id, endtime) { var clock = document.getElementById(id); var daysSpan = clock.querySelector('.days'); var hoursSpan = clock.querySelector('.hours'); var minutesSpan = clock.querySelector('.minutes'); var secondsSpan = clock.querySelector('.seconds'); function updateClock() { var t = getTimeRemaining(endtime); daysSpan.innerHTML = t.days; hoursSpan.innerHTML = ('0' + t.hours).slice(-2); minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); if (t.total <= 0) { clearInterval(timeinterval); } } updateClock(); var timeinterval = setInterval(updateClock, 1000); } var deadline = 'December 31 2018 23:59:59 GMT+0200'; initializeClock('clockdiv', deadline); 
 body{ text-align: center; background: #fff; font-family: sans-serif; font-weight: 100; } #clockdiv{ font-family: sans-serif; color: #fff; display: inline-block; font-weight: 100; text-align: center; font-size: 30px; } #clockdiv > div{ padding: 10px; border-radius: 3px; background: #a9a9a9; display: inline-block; } #clockdiv div > span{ padding: 15px; border-radius: 3px; background: #585858; display: inline-block; } .smalltext{ padding-top: 5px; font-size: 16px; } 
 <div id="clockdiv"> <div> <span class="days"></span> <div class="smalltext">Days</div> </div> <div> <span class="hours"></span> <div class="smalltext">Hours</div> </div> <div> <span class="minutes"></span> <div class="smalltext">Minutes</div> </div> <div> <span class="seconds"></span> <div class="smalltext">Seconds</div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM