<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="开始" id="btns">
<input type="button" value="停止" id="btnt">
<br>
<img src="1.jpg" id="pic"/>
</body>
</html>
<script>
var oBtns = document.getElementById("btns");
var oBtnt = document.getElementById("btnt");
var oPic = document.getElementById("pic");
function rand( min,max ){
return Math.round( Math.random()*(max-min) + min )
}
//点击"开始"按钮, 开始切换图片
oBtns.onclick = function(){
timer = setInterval( function(){
oPic.src = ""+ rand(1,8) +".jpg";
},500 )
}
//点击"停止"按钮, 停止切换图片
oBtnt.onclick = function(){
clearInterval( timer );
}
</script>