BOM(定时器的用法,location对象)

间歇调用(在指定的时间过后执行代码)和超时调用(每隔指定的时间就执行一次代码)。
//这种写法不推荐,容易出错,不容易扩展
setTimeout("alert('Lee')",2000); //2秒后执行第一个参数的代码块

function box(){
alert('Lee');
}
setTimeout(box,2000);

以下是推荐使用的:
var box=setTimeout(function(){
alert('Lee');
},2000);

clearTimeout(box);

-------------------------------------------
var box=setInterval(function(){ //间歇调用,可以重复不断的执行
alert('Lee');
},1000);
clearInterval(box);


var num=0;
var max=5;

setInterval(function(){
num++;
if(num==max){
clearInterval(this); //this代表本身
alert('5秒到了!');
}
},1000);


-------------------------------------------
真正常用的方法是:使用超时调用模拟定时器

var num=0;
var max=5;

function box(){
num++;
document.getElementById('a').innerHTML+=num;
if(num==max){
alert('5秒到了!');
}else{
setTimeout(box,1000);
}
}

setTimeout(box,1000);

--------------------------------------------------------------------------------------

alert(window.location);
alert(window.document.location);
两者等效

location.hash='#66'; //会跳转到新的URL,就是包含#66的URL
alert(location.hash);

alert(location.port); //8080 获取端口号

http://localhost:8080/js/demo3.html?id=5&search=ok
alert(location.search); //取得?之后的
location.search='?id=5'; //如果设置search会不停地跳转 死循环

location.href='http://www.baidu.com'; //跳转

--------------------
function getArgs(){
return location.search;
}

alert(getArgs); //?id=5&search=ok

方法:
function getArgs(){
var args={};
var qs=location.search.length>0?location.search.substring(1):'';
var items=qs.split('&');
var item=null,name=null,value=null;
for(var i=0;i<items.length;i++){
item=items[i].split('=');
name=item[0];
value=item[1];
args[name]=value;
}
return args;
}
var args=getArgs();
alert(args['id']);
alert(args['search']);
--------------------------------------------------
location.assign("http://www.baidu.com"); //跳转
--------------------------------------------------
location.reload(); //最有效的重新加载,有可能从缓存加载
location.reload(true); //强制加载,从服务器源头重新加载

--------------------------------------------------

function a(){
location.replace('http://www.baidu.com'); //不产生任何历史记录的跳转 不可回头
location.href('http://www.baidu.com'); //一般的跳转
}
<a href="javascript:a()">a</a>
--------------------------------------------------

alert(history.length); //历史记录的总量
function back(){
history.back(); history.go(-1)
}
<a href="javascript:back()">prev</a>

history.forward(); //下一页 history.go(1)

posted @ 2017-08-17 09:56  耿鑫  阅读(182)  评论(0编辑  收藏  举报