从开始日期和时间开始倒计时

Countdown timer from a start date and time

本文关键字:开始 倒计时 时间 日期      更新时间:2023-09-26

嗨,我在JS中有以下倒计时计时器,我在页面中使用:

<script type="text/javascript">
     $(function() {
         var count = localStorage.getItem('count') || 60
         countdown = setInterval(function() {
             localStorage.setItem('count'), count);
             $("span.countdown").html(minutes + ":" + seconds + " Remaining");
             if (count == 0) {
                   clearInterval(countdown);
                   localStorage.removeItem('count');
             }
             count--;
         }, 1000);
    });
</script>

我发现,如果用户在我的页面上发出许多PHP帖子请求,计时器并不会真正倒计时。

我想知道是否可以在本地存储中存储日期时间,然后当用户重新加载页面时-计算本地存储中的时间和现在的时间之间的差异-很抱歉缺少代码我是一个完整的JS noob

在PHP中,设置一个会话变量$_SESSION['EndTime']。加载每个页面时,将当前时间与会话变量进行比较,并输出剩余时间。

您可以使用Date对象来获取当前时间:http://www.w3schools.com/js/js_obj_date.asp

示例:

new Date().getTime() // gets the amount of alle milliseconds since 1970 => also usefull for the serialization
new Date(milliseconds) // restoring the date object


我希望这能帮助你