JS内部的Javascript倒计时器+ PHP代码

时间:2014-04-10 07:48:09

标签: javascript php jquery timer countdown

我需要帮助javascript中的倒数计时器。我在PHPJabbers中找到了以下代码。这段代码工作正常,但是当我重新加载页面时,时间会回到初始时间。即使我加载页面,我也想保留时间。如果秒== 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>

2 个答案:

答案 0 :(得分:3)

检查一下,(使用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);

工作jsFiddle

答案 1 :(得分:0)

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;
    }