我有一个功能,当页面打开时自动加载。我想让功能在“onclick”后停止运行。我想让幻灯片在点击一个按钮后自动停止运行。onclick后禁用功能
<html>
<head>
<script type="text/javascript">
function changeImage(x){
imageNumber += x;
if(imageNumber > imageLength){
imageNumber = 0;
}
if(imageNumber < 0){
imageNumber = imageLength;
}
document.getElementById("slideshow").src = images[imageNumber];
document.getElementById("caption").innerHTML = caption[imageNumber];
return false;
}
function autoRun(){
//This function simply makes the slideshow change slides every 5 seconds.
setInterval("changeImage(1)", 5000);
}
function clearFunction(){
//This function needs to stop the autoRun function.
//If the user wants to manual flip through the slides,
//I don't want the slides to continue changing automatically
}
</script>
</head>
<body onload="autoRun()">
<a href="#" onclick="changeImage(-1); clearFunction();">Previous Slide</a>
<a href="#" onclick="changeImage(1); clearFunction();">Next Slide</a>
</body>
</html>
您可以创建一个全局变量。例如:var checkRun = true;您可以在函数中检查此变量,并在第一次运行时将其设置为false。 –
那么autoRun()必须做某种setInterval。您需要在autoRun()之外维护对它的引用,并在clearFunction()中使用clearInterval。请发布autoRun() – vabii