javascript的 setTimeout问题。我想按键图片1,2,3,4.png循环播放,但为什么按键只能变为图片2,不继续了
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"><html><head><title>kadai5</ti...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>kadai5</title>
<script type = "text/javascript">
function changeImg(){
var timer;
var num = 2;
document.myImg.src = "images/ex" + num + ".png";
num++;
if(num == 4) num = 1;
timer = setTimeout("changeImg()",1000);
}
</script>
</head>
<body>
<form>
<input type = "button" value = "スライドショーを见る" onclick = "changeImg()">
<input type = "button" value = "スライドショーを止める" onclick = "">
</form>
<br>
<img src = "images/ex1.png" name = "myImg">
</body>
</html> 展开
<html>
<head>
<title>kadai5</title>
<script type = "text/javascript">
function changeImg(){
var timer;
var num = 2;
document.myImg.src = "images/ex" + num + ".png";
num++;
if(num == 4) num = 1;
timer = setTimeout("changeImg()",1000);
}
</script>
</head>
<body>
<form>
<input type = "button" value = "スライドショーを见る" onclick = "changeImg()">
<input type = "button" value = "スライドショーを止める" onclick = "">
</form>
<br>
<img src = "images/ex1.png" name = "myImg">
</body>
</html> 展开
1个回答
展开全部
改成这样就行了 你哪个把变量定义在了函数里边每次调用函数的时候 num的值都是2 只把要变量定义在外边就行了
<script type = "text/javascript">
var num = 2;
function changeImg(){
var timer;
document.myImg.src = "images/ex" + num + ".png";
num++;
if(num == 4) num = 1;
timer = setTimeout("changeImg()",1000);
}
</script>
<script type = "text/javascript">
var num = 2;
function changeImg(){
var timer;
document.myImg.src = "images/ex" + num + ".png";
num++;
if(num == 4) num = 1;
timer = setTimeout("changeImg()",1000);
}
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询