我有一个在页面打开时自动加载的功能。我希望该函数在“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 interval;
function autoRun(){
//This function simply makes the slideshow change slides every 5 seconds.
interval=setInterval(changeImage, 5000,1);
}
function clearFunction(){
if(interval) clearInterval(interval);
}
关于javascript - onclick后禁用功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44009667/