我试图在 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);