每次重新加载页面时停止重置此脚本

时间:2012-05-14 14:07:01

标签: javascript cookies countdown

我一直在搜索和搜索,但在重新加载页面后无法使此脚本停止重置。

脚本每隔x小时倒计时一次。问题是每次重新加载页面时,计时器都会重置。

有人可以帮我这里,即使重新加载页面,如何保持倒计时?

我打电话给计时器。示例计时器8:> div ... etc id =“timeLeft8”

    <script type="text/javascript">
    var events = new Array();

    events[1]={name: 'Blood Castle:', startUp: new Array(
        {hour:0,minute:25},
        {hour:2,minute:25},
        {hour:4,minute:25},
        {hour:6,minute:25},
        {hour:8,minute:25},
        {hour:10,minute:25},
        {hour:12,minute:25},
        {hour:14,minute:25},
        {hour:16,minute:25},
        {hour:18,minute:25},
        {hour:20,minute:25},
        {hour:22,minute:25}
    )}

    events[2]={name: 'Devil Square:', startUp: new Array(
        {hour:1,minute:55},
        {hour:3,minute:55},
        {hour:5,minute:55},
        {hour:7,minute:55},
        {hour:9,minute:55},
        {hour:11,minute:55},
        {hour:13,minute:55},
        {hour:15,minute:55},
        {hour:17,minute:55},
        {hour:19,minute:55},
        {hour:21,minute:55},
        {hour:23,minute:55}
    )}

    events[3]={name: 'Chaos Castle:', startUp: new Array(
        {hour:0,minute:55},
        {hour:2,minute:55},
        {hour:4,minute:55},
        {hour:6,minute:55},
        {hour:8,minute:55},
        {hour:10,minute:55},
        {hour:12,minute:55},
        {hour:14,minute:55},
        {hour:16,minute:55},
        {hour:18,minute:55},
        {hour:20,minute:55},
        {hour:22,minute:55}
    )}

    events[4]={name: 'Red Dragon:', startUp: new Array(
        {hour:0,minute:0},
        {hour:2,minute:0},
        {hour:4,minute:0},
        {hour:6,minute:0},
        {hour:8,minute:0},
        {hour:10,minute:0},
        {hour:12,minute:0},
        {hour:14,minute:0},
        {hour:16,minute:0},
        {hour:18,minute:0},
        {hour:20,minute:0},
        {hour:22,minute:0}
    )}

    events[5]={name: 'Gold Invasion:', startUp: new Array(
        {hour:0,minute:0},
        {hour:2,minute:0},
        {hour:4,minute:0},
        {hour:6,minute:0},
        {hour:8,minute:0},
        {hour:10,minute:0},
        {hour:12,minute:0},
        {hour:14,minute:0},
        {hour:16,minute:0},
        {hour:18,minute:0},
        {hour:20,minute:0},
        {hour:22,minute:0}
    )}

    events[6]={name: 'White Wizard:', startUp: new Array(
        {hour:0,minute:0},
        {hour:2,minute:0},
        {hour:4,minute:0},
        {hour:6,minute:0},
        {hour:8,minute:0},
        {hour:10,minute:0},
        {hour:12,minute:0},
        {hour:14,minute:0},
        {hour:16,minute:0},
        {hour:18,minute:0},
        {hour:20,minute:0},
        {hour:22,minute:0}
    )}

    events[7]={name: 'Blue:', startUp: new Array(
        {hour:0,minute:40},
        {hour:1,minute:40},
        {hour:2,minute:40},
        {hour:3,minute:40},
        {hour:4,minute:40},
        {hour:5,minute:40},
        {hour:6,minute:40},
        {hour:7,minute:40},
        {hour:8,minute:40},
        {hour:9,minute:40},
        {hour:10,minute:40},
        {hour:11,minute:40},
        {hour:12,minute:40},
        {hour:13,minute:40},
        {hour:14,minute:40},
        {hour:15,minute:40},
        {hour:16,minute:40},
        {hour:17,minute:40},
        {hour:18,minute:40},
        {hour:19,minute:40},
        {hour:20,minute:40},
        {hour:21,minute:40},
        {hour:22,minute:40},
        {hour:23,minute:40},
        {hour:24,minute:40}
    )}

    events[7]={name: 'Hide&Seek:', startUp: new Array(
        {hour:0,minute:42},
        {hour:1,minute:22},
        {hour:2,minute:32},
        {hour:3,minute:42},
        {hour:4,minute:52},
        {hour:6,minute:02},
        {hour:7,minute:12},
        {hour:8,minute:22},
        {hour:9,minute:32},
        {hour:10,minute:42},
        {hour:11,minute:52},
        {hour:13,minute:02},
        {hour:14,minute:12},
        {hour:15,minute:22},
        {hour:16,minute:32},
        {hour:17,minute:42},
        {hour:18,minute:52},
        {hour:20,minute:02},
        {hour:21,minute:12},
        {hour:22,minute:22},
        {hour:23,minute:32}
    )}

    events[8]={name: 'Sky:', startUp: new Array(
        {hour:1,minute:5},
        {hour:4,minute:5},
        {hour:7,minute:5},
        {hour:10,minute:5},
        {hour:13,minute:5},
        {hour:16,minute:5},
        {hour:19,minute:5},
        {hour:23,minute:5}
    )}

    events[9]={name: 'Boss Attack:', startUp: new Array(
        {hour:1,minute:50},
        {hour:4,minute:50},
        {hour:7,minute:50},
        {hour:10,minute:50},
        {hour:13,minute:50},
        {hour:16,minute:50},
        {hour:23,minute:50}
    )}


    events[10]={name: 'Happy Hour:', startUp: new Array(
        {hour:5,minute:5},
        {hour:2,minute:5},
        {hour:8,minute:5},
        {hour:11,minute:5},
        {hour:14,minute:5},
        {hour:17,minute:5},
        {hour:20,minute:5},
        {hour:0,minute:5}
    )}

    events[11]={name: 'Hit and Up:', startUp: new Array(
        {hour:0,minute:20},
        {hour:2,minute:20},
        {hour:5,minute:20},
        {hour:8,minute:20},
        {hour:11,minute:20},
        {hour:14,minute:20},
        {hour:20,minute:20}
    )}

    events[12]={name: 'Raklion:', startUp: new Array(
        {hour:0,minute:15},
        {hour:3,minute:15},
        {hour:6,minute:15},
        {hour:9,minute:15},
        {hour:12,minute:15},
        {hour:15,minute:15},
        {hour:18,minute:15},
        {hour:21,minute:15}
    )}

    events[13]={name: 'Moss:', startUp: new Array(
        {hour:3,minute:35},
        {hour:7,minute:35},
        {hour:11,minute:35},
        {hour:15,minute:35},
        {hour:19,minute:35},
        {hour:23,minute:35}
    )}

    events[14]={name: 'Illusion Temple:', startUp: new Array(
        {hour:0,minute:25},
        {hour:1,minute:25},
        {hour:2,minute:25},
        {hour:3,minute:25},
        {hour:4,minute:25},
        {hour:5,minute:25},
        {hour:6,minute:25},
        {hour:7,minute:25},
        {hour:8,minute:25},
        {hour:9,minute:25},
        {hour:10,minute:25},
        {hour:11,minute:25},
        {hour:12,minute:25},
        {hour:13,minute:25},
        {hour:14,minute:25},
        {hour:15,minute:25},
        {hour:16,minute:25},
        {hour:17,minute:25},
        {hour:18,minute:25},
        {hour:19,minute:25},
        {hour:20,minute:25},
        {hour:21,minute:25},
        {hour:22,minute:25},
        {hour:23,minute:25},
        {hour:24,minute:25}
    )}

    events[15]={name: 'Castle Deep:', startUp: new Array(
        {hour:1,minute:25},
        {hour:7,minute:25},
        {hour:13,minute:25},
        {hour:19,minute:25}
    )}

    events[16]={name: 'CryWolf:', startUp: new Array(
        {hour:1,minute:45},
        {hour:4,minute:45},
        {hour:7,minute:45},
        {hour:10,minute:45},
        {hour:13,minute:45},
        {hour:16,minute:20},
        {hour:19,minute:45},
        {hour:22,minute:45}
    )}

    var curTime=1336998502
    var dateTime=1336953600
    function timeLeft(i){
        for(j in events[i].startUp){
            tmp=events[i].startUp[j].hour*3600+events[i].startUp[j].minute*60
            if(dateTime+tmp>curTime){
                return dateTime+tmp-curTime;
            }
        }
        tmp=events[i].startUp[0].hour*3600+events[i].startUp[0].minute*60
        return dateTime+86400+tmp-curTime;
    }

    function getFormatedLeftTime($seconds){
        $second = $seconds % 60;
        $minutes = parseInt(($seconds / 60) % 60);
        $hour =  parseInt(($seconds / 3600) % 24);
        $days = parseInt($seconds / (24 * 3600));

        $ret = '';
        if ($days > 0)
            if ($days == 1) $ret += '1 day ';
            else $ret += $days + ' days ';


        if ($hour > 0){
            if ($hour < 10) $hour = '0' + $hour;
            $ret += $hour + ':';
        }else if ($days > 0) $ret += '00:';

        if($minutes < 10) $minutes = '0' + $minutes;

        $ret += $minutes + ':';

        if ($second < 10) $second = '0' + $second;

        $ret += $second;
        return $ret;

    }

    function updateTimes(){
        curTime++;
        for (i in events){
            document.getElementById("timeLeft"+i).innerHTML=getFormatedLeftTime(timeLeft(i));
        }
    }

    for(i in events)
        document.getElementById("eventList").innerHTML+="<div style='float:right; color: #FFFF00;' id='timeLeft"+i+"'>"+getFormatedLeftTime(timeLeft(i))+"</div><div style=\"color: #00FFFF;\"><strong>"+events[i].name+"</strong></div>";
    setInterval("updateTimes()", 1000);



    </script>

3 个答案:

答案 0 :(得分:2)

欢迎,javascript和html是无状态的,每一页都是新的一天。

虽然您使用cookie标记了您的问题,但您没有使用它,因此......使用它 无论如何,这些事情应该在服务器端,而不是客户端。

答案 1 :(得分:2)

将计数器值保留在本地存储器中可能吗? 当您的页面加载时,您可以检查它是否存在,如果不是从头开始。

像这样:

var counter = 0;
if(localStorage.counter) {
    counter = Number(localStorage.counter);
} else {
    counter = 1;
}

然后当您更新计数器时,将其保存在存储器中,例如:

counter += 1;
localStorage.counter = counter;

好的,这是在您的脚本中实现的这种技术: (请记住,我不完全确定它的表现如何!)

<script type="text/javascript">
var events = new Array();

events[1]={name: 'Blood Castle:', startUp: new Array(
    {hour:0,minute:25},
    {hour:2,minute:25},
    {hour:4,minute:25},
    {hour:6,minute:25},
    {hour:8,minute:25},
    {hour:10,minute:25},
    {hour:12,minute:25},
    {hour:14,minute:25},
    {hour:16,minute:25},
    {hour:18,minute:25},
    {hour:20,minute:25},
    {hour:22,minute:25}
)}

events[2]={name: 'Devil Square:', startUp: new Array(
    {hour:1,minute:55},
    {hour:3,minute:55},
    {hour:5,minute:55},
    {hour:7,minute:55},
    {hour:9,minute:55},
    {hour:11,minute:55},
    {hour:13,minute:55},
    {hour:15,minute:55},
    {hour:17,minute:55},
    {hour:19,minute:55},
    {hour:21,minute:55},
    {hour:23,minute:55}
)}

events[3]={name: 'Chaos Castle:', startUp: new Array(
    {hour:0,minute:55},
    {hour:2,minute:55},
    {hour:4,minute:55},
    {hour:6,minute:55},
    {hour:8,minute:55},
    {hour:10,minute:55},
    {hour:12,minute:55},
    {hour:14,minute:55},
    {hour:16,minute:55},
    {hour:18,minute:55},
    {hour:20,minute:55},
    {hour:22,minute:55}
)}

events[4]={name: 'Red Dragon:', startUp: new Array(
    {hour:0,minute:0},
    {hour:2,minute:0},
    {hour:4,minute:0},
    {hour:6,minute:0},
    {hour:8,minute:0},
    {hour:10,minute:0},
    {hour:12,minute:0},
    {hour:14,minute:0},
    {hour:16,minute:0},
    {hour:18,minute:0},
    {hour:20,minute:0},
    {hour:22,minute:0}
)}

events[5]={name: 'Gold Invasion:', startUp: new Array(
    {hour:0,minute:0},
    {hour:2,minute:0},
    {hour:4,minute:0},
    {hour:6,minute:0},
    {hour:8,minute:0},
    {hour:10,minute:0},
    {hour:12,minute:0},
    {hour:14,minute:0},
    {hour:16,minute:0},
    {hour:18,minute:0},
    {hour:20,minute:0},
    {hour:22,minute:0}
)}

events[6]={name: 'White Wizard:', startUp: new Array(
    {hour:0,minute:0},
    {hour:2,minute:0},
    {hour:4,minute:0},
    {hour:6,minute:0},
    {hour:8,minute:0},
    {hour:10,minute:0},
    {hour:12,minute:0},
    {hour:14,minute:0},
    {hour:16,minute:0},
    {hour:18,minute:0},
    {hour:20,minute:0},
    {hour:22,minute:0}
)}

events[7]={name: 'Blue:', startUp: new Array(
    {hour:0,minute:40},
    {hour:1,minute:40},
    {hour:2,minute:40},
    {hour:3,minute:40},
    {hour:4,minute:40},
    {hour:5,minute:40},
    {hour:6,minute:40},
    {hour:7,minute:40},
    {hour:8,minute:40},
    {hour:9,minute:40},
    {hour:10,minute:40},
    {hour:11,minute:40},
    {hour:12,minute:40},
    {hour:13,minute:40},
    {hour:14,minute:40},
    {hour:15,minute:40},
    {hour:16,minute:40},
    {hour:17,minute:40},
    {hour:18,minute:40},
    {hour:19,minute:40},
    {hour:20,minute:40},
    {hour:21,minute:40},
    {hour:22,minute:40},
    {hour:23,minute:40},
    {hour:24,minute:40}
)}

events[7]={name: 'Hide&Seek:', startUp: new Array(
    {hour:0,minute:42},
    {hour:1,minute:22},
    {hour:2,minute:32},
    {hour:3,minute:42},
    {hour:4,minute:52},
    {hour:6,minute:02},
    {hour:7,minute:12},
    {hour:8,minute:22},
    {hour:9,minute:32},
    {hour:10,minute:42},
    {hour:11,minute:52},
    {hour:13,minute:02},
    {hour:14,minute:12},
    {hour:15,minute:22},
    {hour:16,minute:32},
    {hour:17,minute:42},
    {hour:18,minute:52},
    {hour:20,minute:02},
    {hour:21,minute:12},
    {hour:22,minute:22},
    {hour:23,minute:32}
)}

events[8]={name: 'Sky:', startUp: new Array(
    {hour:1,minute:5},
    {hour:4,minute:5},
    {hour:7,minute:5},
    {hour:10,minute:5},
    {hour:13,minute:5},
    {hour:16,minute:5},
    {hour:19,minute:5},
    {hour:23,minute:5}
)}

events[9]={name: 'Boss Attack:', startUp: new Array(
    {hour:1,minute:50},
    {hour:4,minute:50},
    {hour:7,minute:50},
    {hour:10,minute:50},
    {hour:13,minute:50},
    {hour:16,minute:50},
    {hour:23,minute:50}
)}


events[10]={name: 'Happy Hour:', startUp: new Array(
    {hour:5,minute:5},
    {hour:2,minute:5},
    {hour:8,minute:5},
    {hour:11,minute:5},
    {hour:14,minute:5},
    {hour:17,minute:5},
    {hour:20,minute:5},
    {hour:0,minute:5}
)}

events[11]={name: 'Hit and Up:', startUp: new Array(
    {hour:0,minute:20},
    {hour:2,minute:20},
    {hour:5,minute:20},
    {hour:8,minute:20},
    {hour:11,minute:20},
    {hour:14,minute:20},
    {hour:20,minute:20}
)}

events[12]={name: 'Raklion:', startUp: new Array(
    {hour:0,minute:15},
    {hour:3,minute:15},
    {hour:6,minute:15},
    {hour:9,minute:15},
    {hour:12,minute:15},
    {hour:15,minute:15},
    {hour:18,minute:15},
    {hour:21,minute:15}
)}

events[13]={name: 'Moss:', startUp: new Array(
    {hour:3,minute:35},
    {hour:7,minute:35},
    {hour:11,minute:35},
    {hour:15,minute:35},
    {hour:19,minute:35},
    {hour:23,minute:35}
)}

events[14]={name: 'Illusion Temple:', startUp: new Array(
    {hour:0,minute:25},
    {hour:1,minute:25},
    {hour:2,minute:25},
    {hour:3,minute:25},
    {hour:4,minute:25},
    {hour:5,minute:25},
    {hour:6,minute:25},
    {hour:7,minute:25},
    {hour:8,minute:25},
    {hour:9,minute:25},
    {hour:10,minute:25},
    {hour:11,minute:25},
    {hour:12,minute:25},
    {hour:13,minute:25},
    {hour:14,minute:25},
    {hour:15,minute:25},
    {hour:16,minute:25},
    {hour:17,minute:25},
    {hour:18,minute:25},
    {hour:19,minute:25},
    {hour:20,minute:25},
    {hour:21,minute:25},
    {hour:22,minute:25},
    {hour:23,minute:25},
    {hour:24,minute:25}
)}

events[15]={name: 'Castle Deep:', startUp: new Array(
    {hour:1,minute:25},
    {hour:7,minute:25},
    {hour:13,minute:25},
    {hour:19,minute:25}
)}

events[16]={name: 'CryWolf:', startUp: new Array(
    {hour:1,minute:45},
    {hour:4,minute:45},
    {hour:7,minute:45},
    {hour:10,minute:45},
    {hour:13,minute:45},
    {hour:16,minute:20},
    {hour:19,minute:45},
    {hour:22,minute:45}
)}

var curTime=1336998502;
var dateTime=1336953600;
if(localStorage.curTime) {
    curTime = Number(localStorage.curTime);
} else {
    // use default set above
}
// not sure if we need to persist dateTime but doing it anyway!
if(localStorage.dateTime) {
    dateTime = Number(localStorage.dateTime);
} else {
    // use default set above
}
function timeLeft(i){
    for(j in events[i].startUp){
        tmp=events[i].startUp[j].hour*3600+events[i].startUp[j].minute*60
        if(dateTime+tmp>curTime){
            return dateTime+tmp-curTime;
        }
    }
    tmp=events[i].startUp[0].hour*3600+events[i].startUp[0].minute*60
    return dateTime+86400+tmp-curTime;
}

function getFormatedLeftTime($seconds){
    $second = $seconds % 60;
    $minutes = parseInt(($seconds / 60) % 60);
    $hour =  parseInt(($seconds / 3600) % 24);
    $days = parseInt($seconds / (24 * 3600));

    $ret = '';
    if ($days > 0)
        if ($days == 1) $ret += '1 day ';
        else $ret += $days + ' days ';


    if ($hour > 0){
        if ($hour < 10) $hour = '0' + $hour;
        $ret += $hour + ':';
    }else if ($days > 0) $ret += '00:';

    if($minutes < 10) $minutes = '0' + $minutes;

    $ret += $minutes + ':';

    if ($second < 10) $second = '0' + $second;

    $ret += $second;
    return $ret;

}

function updateTimes(){
    curTime++;
    localStorage.curTime = curTime; // save current time
    for (i in events){
        document.getElementById("timeLeft"+i).innerHTML=getFormatedLeftTime(timeLeft(i));
    }
}

for(i in events)
    document.getElementById("eventList").innerHTML+="<div style='float:right; color: #FFFF00;' id='timeLeft"+i+"'>"+getFormatedLeftTime(timeLeft(i))+"</div><div style=\"color: #00FFFF;\"><strong>"+events[i].name+"</strong></div>";
setInterval("updateTimes()", 1000);



</script>

答案 2 :(得分:0)

您可以保留计数器服务器端并仅在页面中放置图形计时器