第一种 :
function reSetDevOptType(prodInstId){
var aMainObjId="tabResChg"+"_"+prodInstId;
var aMainObj=document.getElementById(aMainObjId);
if(aMainObj!=null){
if(aMainObj.contentWindow.refreshResStateAfterLoad){
aMainObj.contentWindow.refreshResStateAfterLoad();
clearTimeout();
}else{
window.setTimeout("reSetDevOptType("+prodInstId+");",100);
}
}
}
第二种:
var iIntervalId = null;
iIntervalId = setInterval(function(){
var aMainObjId="tabResChg"+"_"+prodInstId;
var aMainObj=document.getElementById(aMainObjId);
if(aMainObj!=null){
if(aMainObj.contentWindow!=null&&aMainObj.contentWindow!='undefined'){
if(aMainObj.contentWindow.chgColVisible!=null&&aMainObj.contentWindow.chgColVisible!='undefined'){
aMainObj.contentWindow.chgColVisible("RES_STATE",false);
clearInterval(iIntervalId);
}
}
}
}, 100);
setTimeout函数定时执行某函数,可在给函数传参时容易出现问题,这里是自己在网上查询的一些解决方法:
1、采用字符串
setTimeout("start(" + argu + ")",1000)
此方法用于传递字符串,无法传递对象
2、匿名函数
setTimeout(function(){ start(argu)},1000); 调用start函数并传arg参数
字符串、对象都行
3、在有些情况下需在循环中使用,如:
window.οnlοad=function(){
var a=[{id:1},{id:2},{id:3},{id:4}];
for(var i=0;i<3;i++){
setTimeout(function(){start(a[i])},1000);
}
}
function start(obj){ alert(obj.id);}
输出的结果都为 4。因为在一个函数体中,a和i相对于匿名函数是全局的,循环执行完后i=3 ,都执行start(a[3]),所以输出结果相同。
这种情况怎么解决呢,只需把setTimeout封装到一个函数中即可
window.οnlοad=function(){
var a=[{id:1},{id:2},{id:3},{id:4}];
for(var i=0;i<3;i++){
set(a[i]);
}
}
function set(obj){ setTimeout(function(){start(obj)},1000); }
obj和匿名函数在同一作用域,所以在匿名函数中可以直接访问obj,而且每次执行setTimeout时都会产生新的匿名函数体和作用域,所以定时执行匿名函数时调用start并传值就不会出问题了。(此为个人的理解,不一定正确)
function start(obj){ alert(obj.id);}
在网上看到有人封装了一个函数,也可实现该功能
var util={
setTimeout:function(fun, delay) {
if(typeof fun == 'function'){
var argu = Array.prototype.slice.call(arguments,2);
var f = (function(){
fun.apply(null, argu);
}
);
return window.setTimeout(f, delay);
}
return window.setTimeout(fun,delay);
}
}
window.οnlοad=function(){
var a=[{id:1},{id:2},{id:3},{id:4}];
for(var i=0;i<3;i++){
util.setTimeout(start,1000,a[i]);
}
}