4.定时器
定时器:id=setInterval(fun,30); 清除定时器:clearInterval(id);
延时:id=setTimeOut(fun,30); 清除延时:clearTimeOut(id);
str.toCharAt(i);获取字符串str第i个位置上的字符;兼容ie和高级浏览器;
例子:简易时钟;
function toDou(n) { if(n<10) { return '0'+n; } else { return ''+n; } } window.onload=function () { var aImg=document.getElementsByTagName('img'); function tick(){ var oDate=new Date(); var str=toDou(oDate.getHours())+toDou(oDate.getMinutes())+toDou(oDate.getSeconds()); for(var i=0;i<aImg.length;i++) { aImg[i].src='img/'+str.charAt(i)+'.png'; } } setInterval(tick, 1000); tick(); };
字符串连接:str=''+n;
无缝滚动:
window.onload=function () { var oDiv=document.getElementById('div1'); var oUl=oDiv.getElementsByTagName('ul')[0]; var aLi=oUl.getElementsByTagName('li'); oUl.innerHTML=oUl.innerHTML+oUl.innerHTML; oUl.style.width=aLi[0].offsetWidth*aLi.length+'px'; function move(){ if(oUl.offsetLeft<-oUl.offsetWidth/2) { oUl.style.left='0'; } if(oUl.offsetLeft>0) { oUl.style.left=-oUl.offsetWidth/2+'px'; } oUl.style.left=oUl.offsetLeft+2+'px'; } var timer=setInterval(move, 30); oDiv.onmouseover=function () { clearInterval(timer); }; oDiv.onmouseout=function () { timer=setInterval(move, 30); }; };
oUl.innerHTML+=oUl.innerHTML;
延时提示框:
window.onload=function () { var oDiv1=document.getElementById('div1'); var oDiv2=document.getElementById('div2'); var timer=null; oDiv2.onmouseover=oDiv1.onmouseover=function () { clearTimeout(timer); oDiv2.style.display='block'; }; oDiv2.onmouseout=oDiv1.onmouseout=function () { timer=setTimeout(function (){ oDiv2.style.display='none'; }, 500); }; };