如何在一个页面中设置cookie倒数计时器和单选按钮值

时间:2014-02-25 03:30:30

标签: javascript php cookies timer

我想在一个页面中将倒计时cookie和表单cookie组合在一起。顺便说一下如何设置cookie,所以当计时器启动时,所有cookie都被删除了...我是如何设置它的?

这是我的倒数计时器javascript:

    <script type="text/javascript">
var MAX_COUNTER = 900;
var counter = null;
var counter_interval = null;

function setCookie(name,value,days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length,c.length);
        }
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

function resetCounter() {
    counter = MAX_COUNTER;
}

function stopCounter() {
    window.clearInterval(counter_interval);
    deleteCookie('counter');
}

function updateCounter() {
    var msg = '';
    if (counter > 0) {
        counter -= 1;

        hours = parseInt(counter / 3600);
        counter = counter %  3600;

        minutes = parseInt(counter / 60);
        seconds = parseInt(counter % 60);

        msg = hours +" jam :"+ minutes +" menit :"+ seconds +" detik";
        setCookie('counter', counter, 1);
    }
    else {
        msg = "Waktu Habis.";
        stopCounter();
        alert("Waktu Habis.");
        document.form.submit();

    }
    var el = document.getElementById('counter');
    if (el) {
        el.innerHTML = msg;
    }
}

function startCounter() {
    stopCounter();
    counter_interval = window.setInterval(updateCounter, 1000);
}

function init() {
    counter = getCookie('counter');
    if (!counter) {
        resetCounter();
    }
    startCounter();
}

init();

function disable_f5(e){
    if((e.which||e.keyCode)==116){
        e.preventDefault(); 
    }
}
$(document).ready(function(){
    $(document).bind("keydown",disable_f5); 
});
</script>

这是我的cookie形式javascript:

<script type="text/javascript"> 
<!-- 

function getCheckedRad() { 
radNum = getCookie(cookieName) 

if(!radNum){ 
return 
} 

temp=radNum.split("&") 

for(var i=0;i<temp.length;i++){ 
el=document.getElementById("set"+i) 
myInputs=el.getElementsByTagName("INPUT") 
myInputs[temp[i]].checked=true 
} 

} 

function saveCheckedRad(){ 
var exp = new Date() 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000)) 

c=0 
radNum="" 
while(document.getElementById("set"+c)){ 
el=document.getElementById("set"+c) 
myInputs=el.getElementsByTagName("INPUT") 

for(var i=0;i<myInputs.length;i++){ 

if(myInputs[i].checked){ 
radNum+=i+"&" 
} 

} 

c++ 
} 

setCookie(cookieName,radNum,exp) 
} 

cookieName="checked_rad2" 
expDays=365 

function setCookie(name, value, expires, path, domain, secure){// An adaptation of Dorcht's function for setting a cookie. 
if (!expires){expires = new Date()} 
document.cookie = name + "=" + escape(value) + 
((expires == null) ? "" : "; expires=" + expires.toGMTString()) + 
((path == null) ? "" : "; path=" + path) + 
((domain == null) ? "" : "; domain=" + domain) + 
((secure == null) ? "" : "; secure") 
} 

function getCookie(name){ 
var arg = name + "=" 
var alen = arg.length 
var clen = document.cookie.length 
var i = 0 
while (i < clen) { 
var j = i + alen 
if (document.cookie.substring(i, j) == arg) 
return getCookieVal(j) 
i = document.cookie.indexOf(" ", i) + 1 
if (i == 0) break 
} 
return null 
} 

function getCookieVal(offset) { 
var endstr = document.cookie.indexOf (";", offset) 
if (endstr == -1) 
endstr = document.cookie.length 
return unescape(document.cookie.substring(offset, endstr)) 
} 

function deleteCookie(name,path,domain) { // An adaptation of Dorcht's function for deleting a cookie. 
document.cookie = name + "=" + 
((path == null) ? "" : "; path=" + path) + 
((domain == null) ? "" : "; domain=" + domain) + 
"; expires=Thu, 01-Jan-00 00:00:01 GMT" 
} 

// add onload="getCheckedRad()" onunload="saveCheckedRad()" to the opening BODY tag

//--> 
</script> 
抱歉,如果问题太长了......希望你能帮忙...... 我想在一个页面中组合这些javascript,我是如何做到的?

0 个答案:

没有答案