随着时间重复功能

时间:2012-12-04 18:45:44

标签: javascript

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

2 个答案:

答案 0 :(得分:1)

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

在你的情况下它将是

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

<击>

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

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

答案 1 :(得分:1)

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