我想为我的代码设置一个停止和启动按钮。所以我可以打破源代码的工作。
<script language="JavaScript">
var i = 0;
var path = new Array();
// LIST OF IMAGES
path[0] = "http://static.giantbomb.com/uploads/original/0/31/11738-ssj_goku.jpg";
path[1] = "http://i.kinja-img.com/gawker-media/image/upload/arjw8wqvwnihalb6fq3k.png";
path[2] = "http://orig03.deviantart.net/8999/f/2013/167/1/7/sword_art_online_by_sakimichan-d69cxwk.jpg";
function swapImage() {
document.slide.src = path[i];
if (i < path.length - 1)
i++;
else
i = 0;
setTimeout("swapImage()",3000);
}
window.onload=swapImage;
</script>
<img height="200" name="slide" src="image_1.gif" width="400" />
答案 0 :(得分:1)
这将定义一个变量(正在运行),并在点击切换链接时在true和false之间切换:
<!DOCTYPE html>
<html>
<body>
<script language="JavaScript">
var running = true;
var i = 0;
var path = new Array();
// LIST OF IMAGES
path[0] = "http://static.giantbomb.com/uploads/original/0/31/11738-ssj_goku.jpg";
path[1] = "http://i.kinja-img.com/gawker-media/image/upload/arjw8wqvwnihalb6fq3k.png";
path[2] = "http://orig03.deviantart.net/8999/f/2013/167/1/7/sword_art_online_by_sakimichan-d69cxwk.jpg";
function ToggleRunning()
{
running = !running;
if (running)
swapImage();
}
function swapImage()
{
document.slide.src = path[i];
if(i < path.length - 1) i++; else i = 0;
if (running)
setTimeout("swapImage()",3000);
}
window.onload=swapImage;
</script>
<img height="200" name="slide" src="image_1.gif" width="400" />
<button onclick="ToggleRunning();">Toggle</button>
</body>
</html>