<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#div1 {
width: 100px;
height: 100px;
background: red;
}
</style>
</head>
<body>
<input type="button" value='换背景吧' />
<input type="button" value="停"/>
</body>
<script>
var aBtn=document.getElementsByTagName('input');
var arrUrl=['img/1.jpg','img/2.jpg','img/3.jpg','img/4.jpg'];
var num=0;
var oBody=document.body;
//定时器
var timer=null;
aBtn[0].onclick=function(){
//防止多次点击
clearInterval(timer);
timer=setInterval(function(){
//更换背景图片
oBody.style.background='url('+arrUrl[num]+')';
//每次循环增加1,(数组图片,1~2~3~4)
num++;
//判断他的长度是否等于数组的长度
num%=arrUrl.length;
},1000);
}
aBtn[1].onclick=function(){
clearInterval(timer);
}
</script>
</html>