js实现自动刷新控制[基于cookie]

cookie部分是用的别人封装好的。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=
"http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=
"Content-Type" content="text/html; charset=gb2312" />
<title>刷新控制测试</title>
</head>

<body>
<script>

//写cookies函数 作者:翟振凯
function SetCookie(name,value)
//两个参数,一个是cookie的名子,一个是值
{
    var Days = 30;
//此 cookie 将被保存 30 天
    var exp  = new Date();    
//new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name +
"="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)
//取cookies函数        
{
    var arr = document.cookie.match(new RegExp(
"(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)
//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name +
"="+cval+";expires="+exp.toGMTString();
}



SetCookie (
"xiaoqi", "3")
//alert(getCookie('xiaoqi'));
</script>
<input type=
"button" value="reflesh" onclick="reflesh();"/>
<select id=
"times" onchange="changeCookiesTimes();">
<option value=
"5">5</option>
<option value=
"10">10</option>
<option value=
"15">15</option>
</select>
<input type=
"checkbox" value="1" id="chooseAuto" name="chooseAuto" onclick="changeState();" />
<script>
    var isAutoCookie =  getCookie('isAutoCookie');
    var cookieTimes = getCookie('cookieTimes');
var nowTimes = document.getElementById(
"times");
var isAuto = true;
var chooseAuto  =  document.getElementById(
"chooseAuto");
     alert(chooseAuto.checked);

    if (cookieTimes == null) {
    SetCookie (
"cookieTimes", nowTimes.value)
alert(getCookie('cookieTimes'));
} else {
alert('cookiesTimes='+getCookie('cookieTimes'));
}

if (isAutoCookie == null) {
    SetCookie (
"isAutoCookie", chooseAuto.checked)
alert(getCookie('isAutoCookie'));
} else {
alert('isAutoCookie='+getCookie('isAutoCookie'));
}

function changeCookiesTimes() {
alert('nowTimes='+nowTimes.value);
alert('cookiesTimes='+cookieTimes);
if (cookieTimes != nowTimes.value) {
SetCookie (
"cookieTimes", nowTimes.value)
}
}

function changeState() {
var oo  =  document.getElementById(
"chooseAuto");

alert('oo.checked='+oo.checked);

SetCookie (
"isAutoCookie", chooseAuto.checked);
reflesh();
}

document.getElementById(
"times").value = cookieTimes;


if (isAutoCookie=='true')
document.getElementById(
"chooseAuto").checked = true;

if (isAutoCookie=='true') {
setTimeout(
"reflesh()",nowTimes.value*1000);
}

    function reflesh(){

document.location.reload();
}

</script>

</body>
</html>


使用案例
lunzi   2008-11-24 18:48:26 评论:2   阅读:495   引用:0
无题 @2008-11-25 12:59:51  lunzi
是写错了。是做直播的时候,自动设置刷新当前的页面的,如设置隔多长时间自动刷新一次页面好获取新的直播内容。也能设置成手动刷新方式,就是有个控制开关。
无题 @2008-11-24 19:39:26  meiking
reflesh好像写错了,是refresh。这程序是干什么用的啊?

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

认证码: (如不清楚,请刷新)


Copyright@2008 powered by YuLog