0

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

2 回答 2

1

clearInterval(intervalId)是你需要的。

在你的情况下,它会是

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

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

if (timeCountdown <= 0) {
    timeCountdown = 90;
于 2012-12-04T18:49:51.190 回答
1

timeCountdown达到 0 并且您加载 iFrame 时,您需要设置timeCountdown回 90。

于 2012-12-04T18:51:29.167 回答