Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我重新加载页面时,倒计时计时器被重置_Javascript_Jquery - Fatal编程技术网

Javascript 当我重新加载页面时,倒计时计时器被重置

Javascript 当我重新加载页面时,倒计时计时器被重置,javascript,jquery,Javascript,Jquery,我有一个倒计时计时器,它工作得很好,只是当我重新加载页面时,倒计时会从头开始 这是我的HTML <!DOCTYPE html> <html> <head> <title>DrugStore.pk | Under Construction</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type

我有一个倒计时计时器,它工作得很好,只是当我重新加载页面时,倒计时会从头开始

这是我的HTML

<!DOCTYPE html>
 <html>
 <head>
 <title>DrugStore.pk | Under Construction</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>

<!--css-->
<link rel="stylesheet" href="css/style.css" type="text/css">
<link rel="stylesheet" href="css/jquery.countdown.css" />
 <!--/css-->
<!--js-->
<script src="js/jquery.min.js"></script>
<script src="js/jquery.countdown.js"></script>
<script src="js/script.js"></script>
<!--js-->
<!-- webfonts -->
<link href='http://fonts.googleapis.com/css? family=Open+Sans:400,300,700,800' rel='stylesheet' type='text/css'>
<!-- webfonts --> 
</head>
<body> 
<div class="banner">
<div class="wrap">
    <div class="banner-text">
        <h1>Drug Store</h1>
        <h2>Something awesome is coming your way !<br/>Our website is under construction but we are in the final stage of the development program</h2>

        </div>
            <div class="timer_wrap">
                <div id="counter"> </div>       
            </div>  
    </div>              
</div>      
<div class="clear"> </div>
</div> 
<div class="banner-text-info">
<p>Stay in Touch to get the latest updates:</p>

<div class="copy-rights">

</div>
</body>
</html>
这是
jquerycountdown.js

(function($){

// Number of seconds in every time division
var days    = 24*60*60,
    hours   = 60*60,
    minutes = 60;

// Creating the plugin
$.fn.countdown = function(prop){

    var options = $.extend({
        callback    : function(){},
        timestamp   : 0
    },prop);

    var left, d, h, m, s, positions;

    // Initialize the plugin
    init(this, options);

    positions = this.find('.position');

    (function tick(){

        // Time left
        left = Math.floor((options.timestamp - (new Date())) / 1000);

        if(left < 0){
            left = 0;
        }

        // Number of days left
        d = Math.floor(left / days);
        updateDuo(0, 1, d);
        left -= d*days;

        // Number of hours left
        h = Math.floor(left / hours);
        updateDuo(2, 3, h);
        left -= h*hours;

        // Number of minutes left
        m = Math.floor(left / minutes);
        updateDuo(4, 5, m);
        left -= m*minutes;

        // Number of seconds left
        s = left;
        updateDuo(6, 7, s);

        // Calling an optional user supplied callback
        options.callback(d, h, m, s);

        // Scheduling another call of this function in 1s
        setTimeout(tick, 1000);
    })();

    // This function updates two digit positions at once
    function updateDuo(minor,major,value){
        switchDigit(positions.eq(minor),Math.floor(value/10)%10);
        switchDigit(positions.eq(major),value%10);
    }

    return this;
};


function init(elem, options){
    elem.addClass('countdownHolder');

    // Creating the markup inside the container
    $.each(['Days','Hours','Minutes','Seconds'],function(i){
        var boxName;
        if(this=="Days") {
            boxName = "DAYS";
        }
        else if(this=="Hours") {
            boxName = "Hours";
        }
        else if(this=="Minutes") {
            boxName = "Minutes";
        }
        else {
            boxName = "Seconds";
        }
        $('<div class="count'+this+'">' +
            '<span class="position">' +
                '<span class="digit static">0</span>' +
            '</span>' +
            '<span class="position">' +
                '<span class="digit static">0</span>' +
            '</span>' +
            '<span class="boxName">' +
                '<span class="'+this+'">' + boxName + '</span>' +
            '</span>'
        ).appendTo(elem);

        if(this!="Seconds"){
            elem.append('<span class="points">:</span><span class="countDiv countDiv'+i+'"></span>');
        }
    });

}

// Creates an animated transition between the two numbers
function switchDigit(position,number){

    var digit = position.find('.digit')

    if(digit.is(':animated')){
        return false;
    }

    if(position.data('digit') == number){
        // We are already showing this number
        return false;
    }

    position.data('digit', number);

    var replacement = $('<span>',{
        'class':'digit',
        css:{
            top:0,
            opacity:0
        },
        html:number
    });

    // The .static class is added when the animation
    // completes. This makes it run smoother.

    digit
        .before(replacement)
        .removeClass('static')
        .animate({top:0,opacity:0},'fast',function(){
            digit.remove();
        })

    replacement
        .delay(100)
        .animate({top:0,opacity:1},'fast',function(){
            replacement.addClass('static');
        });
}
})(jQuery);
(函数($){
//每次时分的秒数
可变天数=24*60*60,
小时=60*60,
分钟=60;
//创建插件
$.fn.countdown=函数(道具){
变量选项=$.extend({
回调:函数(){},
时间戳:0
},道具);
变量左、d、h、m、s、位置;
//初始化插件
init(此,选项);
positions=this.find('.position');
(功能勾选(){
//剩下的时间
左=Math.floor((options.timestamp-(new Date())/1000);
if(左<0){
左=0;
}
//剩余天数
d=数学楼层(左/天);
updatedoo(0,1,d);
左-=d*天;
//剩余小时数
h=数学楼层(左/小时);
更新(2,3,h);
左-=小时*小时;
//剩余分钟数
m=数学楼层(左/分钟);
更新(4,5,m);
左-=米*分钟;
//剩余秒数
s=左;
更新(6,7,s);
//调用可选的用户提供的回调
选项。回调(d、h、m、s);
//计划在1s内再次调用此函数
设置超时(勾号,1000);
})();
//此函数一次更新两位数的位置
函数updateDo(次要、主要、值){
开关数字(位置均衡器(次要),数学地板(值/10)%10);
开关数字(位置均衡器(主),值%10);
}
归还这个;
};
函数初始化(元素、选项){
元素addClass(“倒计时持有人”);
//在容器内创建标记
$。每个(['Days'、'Hours'、'Minutes'、'Seconds'],函数(i){
var-boxName;
如果(这=“天”){
boxName=“天”;
}
否则,如果(此==“小时”){
boxName=“小时”;
}
否则,如果(此==“分钟”){
boxName=“分钟”;
}
否则{
boxName=“秒”;
}
$('' +
'' +
'0' +
'' +
'' +
'0' +
'' +
'' +
''+boxName+''的+
''
).附录(要素);
如果(此!=“秒”){
元素追加(“:”);
}
});
}
//在两个数字之间创建动画过渡
功能开关数字(位置、编号){
var digit=位置.查找('.digit')
如果(数字为(':animated')){
返回false;
}
if(位置数据('digit')==数字){
//我们已经显示了这个数字
返回false;
}
位置数据('数字',数字);
var替换=$(''{
“类”:“数字”,
css:{
排名:0,
不透明度:0
},
html:数字
});
//.static类是在动画播放时添加的
//完成。这使它运行更平稳。
数字
.之前(更换)
.removeClass('静态')
.animate({top:0,不透明性:0},'fast',function(){
数字删除();
})
替换
.延迟(100)
.animate({top:0,不透明度:1},'fast',function(){
replacement.addClass('static');
});
}
})(jQuery);
每个客户端的持续倒计时计时器 当用户加载页面时,当前代码将开始34天的倒计时。通过将初始时间戳存储在浏览器的本地存储器中,可以使计时器在同一客户端(即同一浏览器)的多个会话中运行

在文件
script.js
的注释
/*----倒计时---*/
下,替换以下行:

$('#counter').countdown({
    timestamp : (new Date()).getTime() + 34*24*60*60*1000
});
var countdownTarget = localStorage.getItem('countdownTarget');    // Read from storage.
if (countdownTarget === null) {                                   // Not stored?
    countdownTarget = (new Date()).getTime() + 34*24*60*60*1000;  // Make a new one.
    localStorage.setItem('countdownTarget', countdownTarget);     // Store it.
}
$('#counter').countdown({
    timestamp : countdownTarget
});
以下几行:

$('#counter').countdown({
    timestamp : (new Date()).getTime() + 34*24*60*60*1000
});
var countdownTarget = localStorage.getItem('countdownTarget');    // Read from storage.
if (countdownTarget === null) {                                   // Not stored?
    countdownTarget = (new Date()).getTime() + 34*24*60*60*1000;  // Make a new one.
    localStorage.setItem('countdownTarget', countdownTarget);     // Store it.
}
$('#counter').countdown({
    timestamp : countdownTarget
});
此代码使用,它在浏览器会话中保持不变。如果希望在用户退出浏览器时忘记时间戳,请改用

固定日期的普遍倒计时 如果您的目标是让所有客户机显示一个计时器,该计时器向下计数到一个固定的时间点,那么您必须在客户机中计算该固定的时间点。您可以在JavaScript中使用,它允许您在中指定日期。UTC大致相当于格林威治标准时间

例如,如果您要倒计时到2015年10月25日UTC 7:00,您可以编写以下内容:

var countdownTarget = Date.UTC(2015, 9, 25, 7);
请注意,month参数是一个介于0和11之间的整数,包括0和11。因此,10月,即第10个月,表示为9。然而,day参数从1开始,因此第25天表示为25天

要在
script.js
中实现此功能,请将
/*----倒计时计时器---*/
下的行替换为:

$('#counter').countdown({
    timestamp : Date.UTC(2015, 9, 25, 7)
});
每个客户端的持久倒计时计时器 当用户加载页面时,当前代码将开始34天的倒计时。通过将初始时间戳存储在浏览器的本地存储器中,可以使计时器在同一客户端(即同一浏览器)的多个会话中运行

在文件
script.js
的注释
/*----倒计时---*/
下,替换以下行:

$('#counter').countdown({
    timestamp : (new Date()).getTime() + 34*24*60*60*1000
});
var countdownTarget = localStorage.getItem('countdownTarget');    // Read from storage.
if (countdownTarget === null) {                                   // Not stored?
    countdownTarget = (new Date()).getTime() + 34*24*60*60*1000;  // Make a new one.
    localStorage.setItem('countdownTarget', countdownTarget);     // Store it.
}
$('#counter').countdown({
    timestamp : countdownTarget
});
以下几行:

$('#counter').countdown({
    timestamp : (new Date()).getTime() + 34*24*60*60*1000
});
var countdownTarget = localStorage.getItem('countdownTarget');    // Read from storage.
if (countdownTarget === null) {                                   // Not stored?
    countdownTarget = (new Date()).getTime() + 34*24*60*60*1000;  // Make a new one.
    localStorage.setItem('countdownTarget', countdownTarget);     // Store it.
}
$('#counter').countdown({
    timestamp : countdownTarget
});
此代码使用,它在浏览器会话中保持不变。如果希望在用户退出浏览器时忘记时间戳,请改用

固定日期的普遍倒计时 如果您的目标是让所有客户机显示一个计时器,该计时器向下计数到一个固定的时间点,那么您必须在客户机中计算该固定的时间点。您可以在JavaScript中使用,它允许您在中指定日期。UTC大致相当于格林威治标准时间

例如,如果您要倒计时到2015年10月25日UTC 7:00,您可以编写以下内容:

var countdownTarget = Date.UTC(2015, 9, 25, 7);
请注意,month参数是一个介于0和11之间的整数,包括0和11。因此,10月,即第10个月,表示为9。然而,day参数从1开始,因此第25天表示为25天

要实现这一点