18

setTimeout火狐下传参数需注意

Posted by 晴云孤魂 on 2011 年 06 月 28 日 in web development |

var indexes=document.getElementsByTagName(“*”);

for (i=0;i<indexes.length;i++){
with (indexes[i]){
style.position=”relative”;
style.zIndex=0;
}
}

var hidetime=null;
var nohide=null;
var TipsHeader=”<div style=\”height:28px;width:108px;background-color:#519319;line-height:28px;font-size:13px;\”><div style=\”height:28px;float:left;width:108px;line-height:28px;text-align:center\”>QQ在线咨询</div></div>”;
var QQLayer=document.createElement(“div”);
StartQQ();
function StartQQ(){
with (QQLayer){
id=”my_qqlayer”;
style.display=””;
style.position=”absolute”;
style.zIndex=2000;
style.backgroundColor=”#CCCCCC”;
style.top=getTopPos().top+”px”;
style.left=getLeftPos().left+”px”;
style.width=getLeftPos().width+”px”;
style.height=getTopPos().height+”px”;
innerHTML=TipsHeader;
innerHTML+='<div style=”height:171px;width:108px;list-style:none; text-align:center;line-height:32px;padding-top:19px;border:#519319 2px solid;overflow:hidden”><ul style=”list-style:none; margin:0px”><li style=”list-style:none;height:30px”><a target=”_blank” href=”http://wpa.qq.com/msgrd?v=3&uin=2413880991&site=qq&menu=yes”><img border=”0″ src=”http://wpa.qq.com/pa?p=2:2413880991:41″ alt=”点击这里给我发消息” title=”点击这里给我发消息”></a></li><li style=”list-style:none;height:30px”><a target=”_blank” href=”http://wpa.qq.com/msgrd?v=3&uin=1983114967&site=qq&menu=yes”><img border=”0″ src=”http://wpa.qq.com/pa?p=2:1983114967:41″ alt=”点击这里给我发消息” title=”点击这里给我发消息”></a></li><li style=”list-style:none;height:30px”><a target=”_blank” href=”http://wpa.qq.com/msgrd?v=3&uin=2455298275&site=qq&menu=yes”><img border=”0″ src=”http://wpa.qq.com/pa?p=2:2455298275:41″ alt=”点击这里给我发消息” title=”点击这里给我发消息”></a></li><li style=”list-style:none;height:30px”><a target=”_blank” href=”http://wpa.qq.com/msgrd?v=3&uin=455105374&site=qq&menu=yes”><img border=”0″ src=”http://wpa.qq.com/pa?p=2:455105374:41″ alt=”点这里给我发消息” title=”点击这里给我发消息”></a></li></ul><span style=”font-size:12px;text-align:right”><a href=”javascript:void(0)” onclick=”HideQQ()”>点击收起咨询台</a></span></div>’;
}
if (!document.getElementById(“my_qqlayer”)) document.body.appendChild(QQLayer);
//document.getElementById(“close_lit”).onclick=CloseQQ;
try{
clearInterval(hidetime);
//这里一开始写作setInterval(SetTopPos,10);发现在火狐下不停抖动,因为下面的SetTopPos我加了参数判断,火狐会默认会传值给函数,调试后不得不纠正…
nohide=setInterval(“SetTopPos()”,10);
}catch(e){}
}

function CloseQQ(){
try{
var closediv=document.getElementById(“my_qqlayer”);
closediv.style.display=”none”;
document.body.removeChild(closediv);
}catch(e){}

}

function getLeftPos(ishide){
//定义宽度
if (ishide){
this.width=60;
}else{
this.width=108;
}
var clientWidth=document.body.clientWidth||document.documentElement.clientWidth;
return{
width:this.width,
left:clientWidth-this.width
}
}

function getTopPos(ishide){
//定义高度
var addheight;
if (ishide){
this.height=20;
addheight=250;
}else{
this.height=220;
addheight=this.height;
}
var scroll=window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
return{
height:this.height,
top:scroll+addheight
}
}

function SetTopPos(ishide){
if (ishide){
QQLayer.style.top=getTopPos(1).top+”px”;
QQLayer.style.left=getLeftPos(1).left+”px”;
}else{
QQLayer.style.top=getTopPos().top+”px”;
QQLayer.style.left=getLeftPos().left+”px”;
}
}
/*
try{
window.onscroll=SetTopPos;
}catch(e){
try{
document.body.onscroll=SetTopPos;
}
catch(e){}
}

*/
function HideQQ(){
with(QQLayer){
style.width=getLeftPos(1).width+”px”;
style.height=getTopPos(1).height+”px”;
style.top=getTopPos(1).top+”px”;
style.left=getLeftPos(1).left+”px”;
}
QQLayer.innerHTML=”<div style=\”width:60px;height:20px;line-height:20px;background-color:#519319;font-size:13px;cursor:pointer;\” onclick=\”StartQQ()\”>点击展开</div>”;
clearInterval(nohide);
hidetime=setInterval(“SetTopPos(1)”,10);
}

======================================================================

参看文献:

最近才发现setTimeout在火狐下的怪异现象

  1. <script>
  2. var c = 1;
  3. var r = function(k){
  4. alert(k)
  5. c = k==undefined?c:k;
  6. alert(c);
  7. c++;
  8. repeat = setTimeout(r,2000)
  9. }
  10. setTimeout(r,2000);
  11. </script>

复制代码

照常理应该是弹出 undefined, 1, undefined, 2 …

但是火狐会怪异的弹出 0, 0, -1, -1 之类的奇怪数字,k是不存在的,却可以得到随机数字。

后来发现问题出在 setTimeout的调用的函数引用上

setTimeout(r,1000)

如果写成

setTimeout(“r()”,1000)

或者

  1. setTimeout(function(){
  2. r()
  3. },1000)

复制代码

便没有问题。

仅火狐有这个问题

firefox对setTimeout的解释

https://developer.mozilla.org/Talk:en/DOM/window.setTimeout

火狐会自动为setTimeout的函数传入一个最末参数, lateness , 不知如何解释

Copyright © 2011-2024 晴云孤魂's Blog All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.

普人特福的博客cnzz&51la for wordpress,cnzz for wordpress,51la for wordpress