【问题标题】:Jquery / JavaScript Countdown Timer From Passed Hour, Minutes and Seconds Variables来自经过的小时、分钟和秒变量的 Jquery / JavaScript 倒数计时器
【发布时间】:2020-05-06 01:40:41
【问题描述】:

好的,我在This Post 找到了此代码,我希望添加小时以及分钟和秒。 有人可以帮忙吗 - 我对 jquery 不是很好:(

代码如下:

<div id=timer></div>
<script type="text/javascript">
    var timeoutHandle;
    function countdown(minutes, seconds) {
        function tick() {
            var counter = document.getElementById("timer");
            counter.innerHTML =
                minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
            seconds--;
            if (seconds >= 0) {
                timeoutHandle = setTimeout(tick, 1000);
            } else {
                if (minutes >= 1) {
                    // countdown(mins-1);   never reach “00″ issue solved:Contributed by Victor Streithorst
                    setTimeout(function () {
                        countdown(minutes - 1, 59);
                    }, 1000);
                }
            }
        }
        tick();
    }

    countdown(1, 30);
</script>

所以,要明确一点,我需要传递 3 个变量:countdown(hour, mins, secs); - 非常感谢!

【问题讨论】:

    标签: javascript jquery countdown countdowntimer


    【解决方案1】:

    所以在玩了一段时间后,我想我已经弄明白了。 我不确定这是否是最好的方法,但从我的测试来看,它似乎可以工作长达 24 小时。

    这里是代码;如果您可以添加或改进它,请像我对 java 脚本不太擅长一样 :(

    <div id=timer></div>
    <script type="text/javascript">
    var timeoutHandle;
    function countdown(hours, minutes, seconds) {
    function tick() {
    var counter = document.getElementById("timer");
    counter.innerHTML = hours.toString() + ":" + (minutes < 10 ? "0" : "") + String(minutes) + ":" + (seconds < 10 ? "0" : "") + String(seconds);
    seconds--;
    if (seconds >= 0) {
    timeoutHandle = setTimeout(tick, 1000);
    } else {
    if (minutes >= 1) {
    setTimeout(function () { countdown(hours, minutes - 1, 59); }, 1000);
    }
    else if (hours >= 1) {
    setTimeout(function () { countdown(hours - 1, 59, 59); }, 1000);
    }
    
    }
    }
    tick();
    }
    
    countdown(2,30,10);
    </script>
    

    我希望有人觉得这很有用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多