定时器
- setInterval(函数,毫秒); 重复执行
- clearIntrval(setInterval(函数,毫秒)) 停止
body{
<input type="button" value="按钮" id="btn1" />
<script type="text/javascript">
var Btn1=document.getElementsByTagName('input');
var oBody=document.body;
var timer=null;
var Arr=['img/3.jpg','img/4.jpg','img/bg.jpg'];
var num=0;
Btn1[0].onclick=function(){
clearInterval(timer);
timer= setInterval(function(){
oBody.style.background='url('+Arr[num]+')';
//oDiv1.style.background='url(img/4.jpg)';
num++;
num%=Arr.length;
},1000);
};
Btn1[1].onclick=function(){
clearInterval(timer);
};
</script>
}
- setTimeout(函数,毫秒); 执行一次
- clearTimeout(函数,毫秒); 停止
<body>
<img id="img1" src="img/4.jpg" style="display: none;"/>
<input type="button" value="关闭" id="btn1" style="display: none;"/>
<script>
var oImg1=document.getElementById('img1');
var oBtn1=document.getElementById('btn1');
var timer=null;
timer= setTimeout(function(){
oImg1.style.display='inline-block';
oBtn1.style.display='block';
setTimeout(function(){
oImg1.style.display='none';
},5000);
},2000);
oBtn1.onclick=function(){
oImg1.style.display='none';
};
</script>
</body>