gpt4 book ai didi

javascript - 随时间重复的函数

转载 作者:行者123 更新时间:2023-12-02 19:11:02 25 4
gpt4 key购买 nike

我试图在 iframe 中每 90 秒显示一次体育比分。除了一部分之外,我一切都正常。我遇到的问题是,一旦时间从 90 变为 0,它就会将 html 页面加载到 iframe 中,并继续永远重新加载相同的 html 页面。如何让它加载一次,然后重新开始 90 秒倒计时?

// variables
var timeCountdown = 90,
minutesCountdown,
secondsCountdown,
mainCountdown;
// main script countdown
mainCountdown = setInterval(function() {
// display countdown time
document.getElementById("countdown").innerText = mainCountdown + " seconds remaining!";
// if time is 0 or smaller
if (timeCountdown <= 0) {
// load sports scores
document.getElementById("iframe").src = "sports/nba/feed.html";
} else {
// convert random time to minutes
minutesCountdown= Math.floor(timeCountdown / 60);
// convert random time to seconds
secondsCountdown = (timeCountdown % 60);
// add leading zero to seconds if less than ten
if (secondsCountdown < 10) {
secondsCountdown = "0" + secondsCountdown;
}
// add leading zero to minutes if less than ten
if (minutesCountdown <= 10) {
minutesCountdown = "0" + minutesCountdown;
}
// display countdown until next search
document.getElementById("countdown").innerHTML = "Loading NBA scores in " + minutesCountdown + ':' + secondsCountdown;
// reduce countdown time
timeCountdown--;
}
}, 1000);

最佳答案

<罢工> clearInterval(intervalId)就是您所需要的。

在你的情况下是

if (timeCountdown <= 0) {
window.clearInterval(mainCountdown);

<罢工>

哦,你想重置计时器吗?将变量 timeCountdown 设置回 90!

if (timeCountdown <= 0) {
timeCountdown = 90;

关于javascript - 随时间重复的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13709789/

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