JS 中的 Javascript 倒数计时器 + PHP 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22981833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:07:15  来源:igfitidea点击:

Javascript countdown timer + PHP code inside JS

javascriptphpjquerytimercountdown

提问by rrr

I need help with countdown timer in javascript.I found the code below in PHPJabbers. This code works fine, but when I reload the page the time goes back to its initial time. I want to retain the time even if I load the page. I want also to add PHP function inside if seconds == 0. I'm not sure if it's possible. Thanks in advance

我需要 javascript 中的倒数计时器帮助。我在 PHPJabbers 中找到了下面的代码。这段代码工作正常,但是当我重新加载页面时,时间又回到了初始时间。即使我加载页面,我也想保留时间。我还想在 if seconds == 0 内添加 PHP 函数。我不确定是否可能。提前致谢

<span id="countdown" class="timer"></span>
<script>
var seconds = 60;
    function secondPassed() {
    var minutes = Math.round((seconds - 30)/60);
    var remainingSeconds = seconds % 60;
    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds; 
    }
    document.getElementById('countdown').innerHTML = minutes + ":" +    remainingSeconds;
    if (seconds == 0) {
        clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "Buzz Buzz";
    } else {    
        seconds--;
    }
    }
var countdownTimer = setInterval('secondPassed()', 1000);
</script>

回答by Bhavesh

Check this, (implemented using cookies)

检查这个,(使用cookies实现)

                function setCookie(cname,cvalue,exdays)
                {
                var d = new Date();
                d.setTime(d.getTime()+(exdays*24*60*60*1000));
                var expires = "expires="+d.toGMTString();
                document.cookie = cname + "=" + cvalue + "; " + expires;
                }
                function getCookie(cname)
                {
                var name = cname + "=";
                var ca = document.cookie.split(';');
                for(var i=0; i<ca.length; i++)
                  {
                  var c = ca[i].trim();
                  if (c.indexOf(name)==0) return c.substring(name.length,c.length);
                  }
                return "";
                }

                //check existing cookie
                cook=getCookie("my_cookie");

                if(cook==""){
                   //cookie not found, so set seconds=60
                   var seconds = 60;
                }else{
                     seconds = cook;
                     console.log(cook);
                }

                function secondPassed() {
                    var minutes = Math.round((seconds - 30)/60);
                    var remainingSeconds = seconds % 60;
                    if (remainingSeconds < 10) {
                        remainingSeconds = "0" + remainingSeconds;
                    }
                    //store seconds to cookie
                    setCookie("my_cookie",seconds,5); //here 5 is expiry days

                    document.getElementById('countdown').innerHTML = minutes + ":" +    remainingSeconds;
                    if (seconds == 0) {
                        clearInterval(countdownTimer);
                        document.getElementById('countdown').innerHTML = "Buzz Buzz";
                    } else {    
                        seconds--;
                    }
                }

                var countdownTimer = setInterval(secondPassed, 1000);

working jsFiddle

工作jsFiddle

回答by Samuel Joy

Nice solution by bhavesh.. If someone wants to show the days,hour,mins, seconds together then they can refer this code.

bhavesh 的不错的解决方案。如果有人想一起显示天数、小时数、分钟数、秒数,那么他们可以参考此代码。

Reverse timer from javascript:

var target_date = new Date('Aug 01 2014 20:47:00').getTime();
    var days, hours, minutes, seconds;
    var countdown = document.getElementById('timeremaining');
    var countdownTimer = setInterval(function () {
        var current_date = new Date().getTime();
        var seconds_left = (target_date - current_date) / 1000;

        days = parseInt(seconds_left / 86400);
        seconds_left = seconds_left % 86400;

        hours = parseInt(seconds_left / 3600);
        seconds_left = seconds_left % 3600;
        minutes = parseInt(seconds_left / 60);
        seconds = parseInt(seconds_left % 60);

        if(days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0 )
        {
           document.getElementById('timeremaining').innerHTML = '';
           clearInterval(countdownTimer);              
        }
        else
        {       
            if(days>0)
              {
                 days= days+'d,';
              } 
              else
              {
                 days='';
              }            
            countdown.innerHTML ='( ' + days + checkTime(hours) + ':'+ checkTime(minutes) + ':' + checkTime(seconds) +' remaining)';    
        }
    }, 1000);

    function checkTime(i) {
        if (i < 10) {i = '0' + i};  
        return i;
    }