cookie
cookie的作用:在本地浏览器存储数据。常用于记住账号等
cookie的组成:键值对的形势存储的数据:”userId=123456; psd=123345; phone=1875455”有效期:”expires=今天以后的时间”存储cookie:按照cookie的格式写好一个字符串 然后将它赋值给document.cookie,浏览器就存了这个cookie。可以存储多个键值对,但是键的名字不能重复。var psd = document.getElementById(“psd”).value; var date = new Date(); date.setDate(date.getDate() + 7);// var cookieText =“userId=”+userId+";"+“expires=”+date; var cookieText =“psd=”+psd+";"+“expires=”+date; document.cookie = cookieText;获取cookie:通过document.cookie就能得到浏览器之前存储的cookie。是一个字符串。将这个字符串进行解析,得到自己想要的内容。var data = document.cookie;var result = [];result = data.split("; “);var userIdData = result[0].split(”=")[1];var psdData = result[1].split("=")[1];var userId = document.getElementById(“userId”);userId.value = userIdData;var psd = document.getElementById(“psd”);psd.value = psdData;清除cookie:将存储的cookie的有效期改为过去的某一天,就清除了cookievar date = new Date();date.setDate(date.getDate() - 7);var cookieText = “psd=;expires=” + date;document.cookie = cookieText;
计时事件
setInterval("函数”,“毫秒数”);//计时器,就是每隔多长时间就调用一次函数。例如时钟的演示:
var timer = setInterval(“getTime()”,1000);
function getTime(){
var time = new Date();
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
var p1 = document.getElementById(“p1”);
p1.innerHTML = hour + “:” + minute + “:” + second;
}
clearInterval(计时器);//停止一个计时器。
setTimeout(“函数名”,”毫秒数”);//延时器,就是隔了多长时间后调用一次函数。
var timer = setTimeout(“alarmClock()”,3000);
function alarmClock(){
var p1 = document.getElementById(“p1”);
p1.innerHTML = “”;
}