[英]turning function to occur every 5 seconds instead of every button click
需要讓以下代碼每 5 秒運行一次,而不是每次單擊按鈕(語言:html):
<script>
function goFwd(e) {
reset();
if (e.target.classList.contains("next") && currentImage < thumbnails.length-1) {
currentImage += 1;
fullSizeImgs[currentImage].classList.add('show');
caption.textContent = thumbnails[currentImage].firstElementChild.getAttribute('alt');
hiLiteThumbnail();
} else if (e.target.classList.contains("next") && currentImage === thumbnails.length-1) {
currentImage = 0;
fullSizeImgs[currentImage].classList.add('show');
caption.textContent = thumbnails[currentImage].firstElementChild.getAttribute('alt');
hiLiteThumbnail();
}
}
</script>
您可以使用 javascript 以 5 秒完成循環。
<script>
window.setInterval(function(){
goFwd(e)
}, 5000);
</script>
當你想停止循環時,你可以使用: clearInterval()
您可以使按鈕在 5 秒后自動單擊。 然后您的 function 將每 5 秒自動調用一次。
<script>
var btn = document.getElementById('your_button_id');
setInterval(function(){
btn.click();
}, 5000); // this will make it click again every 5 seconds
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.