JavaScript 和 jQuery 连续倒计时

huangapple go评论81阅读模式
英文:

Javascript and jQuery consecutive countdown

问题

我找到了一个倒计时脚本

JS FIDDLE

我如何使用像这样的 JSON 对象来更改小时、分钟、秒和标题?

[
    {
        "id": "1",
        "title": "倒计时 1",
        "hour": "0",
        "minute": "0",
        "second": "10"
    },
    {
        "id": "2",
        "title": "倒计时 2",
        "hour": "0",
        "minute": "0",
        "second": "10"
    },
    {
        "id": "3",
        "title": "倒计时 3",
        "hour": "0",
        "minute": "0",
        "second": "10"
    }
]

然后逐个连续显示它们,全部倒计时结束后进行重定向?

我已经在使用 foreach 循环,但它破坏了倒计时。这个脚本与使用日期的其他倒计时不同。

英文:

I found a countdown script

JS FIDDLE

How do I change hours, minutes, second and title with JSON object like this?

[
    {
        "id": "1",
        "title": "countdown 1",
        "hour": "0",
        "minute": "0",
        "second": "10"
    },
    {
        "id": "2",
        "title": "countdown 2",
        "hour": "0",
        "minute": "0",
        "second": "10"
    },
    {
        "id": "3",
        "title": "countdown 3",
        "hour": "0",
        "minute": "0",
        "second": "10"
    }
]

and show it one by one consecutively then redirect when all countdown done?

I am already using foreach loop, but it broke the countdown. This script is different than other countdown that use date.

答案1

得分: 1

你不想使用for循环,而是想要一个可以使用if语句来继续或停止的递归函数。

对于我的回答,我有curTimer变量,它代表循环中当前定时器的索引。

maxSeconds代表定时器应持续的时间,基于定时器中的信息。

thisTimer代表数组中选定定时器的数据。

如果当前定时器的索引小于数组长度减1(数组从0开始),则继续,否则输出done。

英文:

You don't want to use a for loop, instead you want a recursive function that can use if statements to continue or not.

For my answer, I have variables for the curTimer which represents the current timer's index in the loop.

maxSeconds represents how long the timer should go for based on the information in the timer.

thisTimer represents the data in the selected timer in the array.

If the current timer's index is less than the array.length -1 (arrays start at 0), then continue, if not then output done.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const output = document.querySelector(&quot;.output&quot;);
let interval;
let curTimer = 0;
let maxSeconds = 0;
let thisTimer;
const timers = [{
    &quot;id&quot;: &quot;1&quot;,
    &quot;title&quot;: &quot;countdown 1&quot;,
    &quot;hour&quot;: &quot;0&quot;,
    &quot;minute&quot;: &quot;0&quot;,
    &quot;second&quot;: &quot;4&quot;
  },
  {
    &quot;id&quot;: &quot;2&quot;,
    &quot;title&quot;: &quot;countdown 2&quot;,
    &quot;hour&quot;: &quot;0&quot;,
    &quot;minute&quot;: &quot;0&quot;,
    &quot;second&quot;: &quot;4&quot;
  },
  {
    &quot;id&quot;: &quot;3&quot;,
    &quot;title&quot;: &quot;countdown 3&quot;,
    &quot;hour&quot;: &quot;0&quot;,
    &quot;minute&quot;: &quot;0&quot;,
    &quot;second&quot;: &quot;4&quot;
  }
]

const displayTime = () =&gt; {
  output.innerHTML = maxSeconds + &quot; timer &quot; + thisTimer.title ;
};


const timer = (timers, inc) =&gt; {
  thisTimer = timers[inc] 
  maxSeconds = (parseInt(thisTimer.hour) * 3600) + (parseInt(thisTimer.minute) * 60) + parseInt(thisTimer.second);

  interval = setInterval(() =&gt; {
    displayTime();
    maxSeconds--

    if (maxSeconds &lt; 0) {
      clearInterval(interval);
      if (curTimer &lt; timers.length-1) {
        curTimer++;
        timer(timers, curTimer);
      }
      else{
        output.innerHTML = &quot;DONE&quot;;
      }
    }
  }, 1000);
};

timer(timers, curTimer);

<!-- language: lang-html -->

&lt;div class=&quot;output&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月19日 12:40:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定