重新开始倒计时器,每当新用户登陆页面时,将其设置为120秒。

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

How do I restart countdown timer to 120 seconds every time a new user lands on the page?

问题

<style>
    #countdown {
        font-size: 36px;
        font-weight: bold;
        color: white;
        text-align: center;
        margin: 20px 0;
    }
</style>

<p id="countdown"></p>

<script>
    function updateCountdown() {
        let startTime = new Date("2023-02-28T15:22:00");
        const currentTime = new Date();
        let diffTime = startTime - currentTime;
        let totalSeconds = diffTime / 1000;

        while (totalSeconds <= 0) {
            startTime.setDate(startTime.getDate() + 7);
            diffTime = startTime - currentTime;
            totalSeconds = diffTime / 1000;
        }

        const days = Math.floor(totalSeconds / (24 * 60 * 60));
        const hours = Math.floor((totalSeconds % (24 * 60 * 60)) / (60 * 60));
        const minutes = Math.floor((totalSeconds % (60 * 60)) / 60);
        const seconds = Math.floor(totalSeconds % 60);

        document.getElementById("countdown").innerHTML = `${days}d ${hours}h ${minutes}m ${seconds}s`;
    }

    setInterval(updateCountdown, 1000);
</script>
英文:

I am trying to use this snippet to refresh a countdown timer to 120 seconds when a new user lands on the page. Any help is appreciated.

I am having trouble on two things: how to set the start date to the current time and then to have this countdown from 120 seconds vs. 7 days.

&lt;style&gt;
    #countdown {
        font-size: 36px;
        font-weight: bold;
        color: white;
        text-align: center;
        margin: 20px 0;
    }
&lt;/style&gt;

&lt;p id=&quot;countdown&quot;&gt;&lt;/p&gt;

&lt;script&gt;
    function updateCountdown() {
        let startTime = new Date(&quot;2023-02-28T15:22:00&quot;);
        const currentTime = new Date();
        let diffTime = startTime - currentTime;
        let totalSeconds = diffTime / 1000;

        while (totalSeconds &lt;= 0) {
            startTime.setDate(startTime.getDate() + 7);
            diffTime = startTime - currentTime;
            totalSeconds = diffTime / 1000;
        }

        const days = Math.floor(totalSeconds / (24 * 60 * 60));
        const hours = Math.floor((totalSeconds % (24 * 60 * 60)) / (60 * 60));
        const minutes = Math.floor((totalSeconds % (60 * 60)) / 60);
        const seconds = Math.floor(totalSeconds % 60);

        document.getElementById(&quot;countdown&quot;).innerHTML = `${days}d ${hours}h ${minutes}m ${seconds}s`;
    }

    setInterval(updateCountdown, 1000);
&lt;/script&gt;

答案1

得分: 0

你可以使用

document.addEventListener('DOMContentLoaded', function() {
// 重置计时器
});

像这样
英文:

You can use

document.addEventListener(&#39;DOMContentLoaded&#39;, function() {
   // reset timer
});

like this

huangapple
  • 本文由 发表于 2023年6月29日 07:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577191.html
匿名

发表评论

匿名网友

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

确定