gpt4 book ai didi

javascript - 1 分钟后停止 setTimeout。环形

转载 作者:行者123 更新时间:2023-11-28 07:01:02 27 4
gpt4 key购买 nike

如何在 1 分钟后停止 setTimeout。由于循环,它继续运行。TIA

<script type="text/javascript">    
var image1 = new Image()
image1.src = "images/slide1.jpg"
var image2 = new Image()
image2.src = "images/slide2.jpg"
</script>
<body>
<p><img src="images/pentagg.jpg" width="500" height="300" name="slide" /></p>
<script type="text/javascript">
var step=1;
function slideit()
{
document.images.slide.src = eval("image"+step+".src");
if(step<2)
step++;
else
step=1;
setTimeout("slideit()",2500);
}
slideit();
</script>

我尝试了很多方法,但仍然无法解决这个问题。我将不胜感激任何帮助。谢谢

最佳答案

尝试命名的setTimeout:

var image1 = new Image();
image1.src = "images/slide1.jpg";
var image2 = new Image();
image2.src = "images/slide2.jpg";

var step = 1, timer; // declare here

function slideit() {

if(timer){ // check if there is any timeout been set
setTimeout(function(){
clearTimeout(timer); // clear the timer here
},5000); // change it to 60*1000 for 1 min == 60 sec == 60000 ms
}

document.images.slide.src = eval("image" + step + ".src");
if (step < 2)
step++;
else
step = 1;
timer = setTimeout("slideit()", 500); // assign a timer here
}
slideit();
<p>
<img src="images/pentagg.jpg" width="500" height="300" name="slide" />
</p>

关于javascript - 1 分钟后停止 setTimeout。环形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32138192/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com