ds1379551 2013-12-12 22:44
浏览 31

即使页面刷新,如何使多重计时器不在PHP中重置?

I already check the answer from here and so how can I make it for multiple data. In order for me to set a timer for a specific service for example. So here is my script.

            <script type="text/javascript">
            //var minutes2 = currentTime.setMinutes(currentTime.getMinutes() + 30);
            function addMinutes(date, minutes) {
                return new Date(date.getTime() + minutes*60000);
            }
            var dat = new Date();
            //var tim = addMinutes(dat, 15);
            //console.log(tim); 

            function fixIntegers(integer)
            {
                if (integer < 0)
                    integer = 0;
                if (integer < 10)
                    return "0" + integer;
                return "" + integer;
            }

            $(function () { 
                $('.btn-start-timer').click(function(){
                    var i = window.setInterval(function(){

                        var future = addMinutes(dat, 60);
                        //var future = new Date("Dec 12 2013 22:10:00 GMT-0800 (Pacific Standard Time) ");
                        var now = new Date();
                        //console.log(tim);
                        var difference = Math.floor((future.getTime() - now.getTime()) / 1000);

                        var seconds = fixIntegers(difference % 60);
                        difference = Math.floor(difference / 60);

                        var minutes = fixIntegers(difference % 60);
                        difference = Math.floor(difference / 60);

                        var hours = fixIntegers(difference % 24);
                        difference = Math.floor(difference / 24);

                        var days = difference;
                    /*  $(this).parent().parent().find('.timer #seconds').text(seconds + "s");
                        $(this).parent().parent().find('.timer #minutes').text(minutes + "m");
                        $(this).parent().parent().find('.timer #hours').text(hours + "h");
                        $(this).parent().parent().find('.timer #days').text(days + "d");*/

                        $(".timer #seconds").text(seconds + "s");
                        $(".timer #minutes").text(minutes + "m");
                        $(".timer #hours").text(hours + "h");
                        $(".timer #days").text(days + "d");
                        if(hours == 0 && minutes == 0 && seconds == 0){
                             window.clearInterval( i );   
                            alert('times up');
                        }
                    }, 1000);

                });
            });

        </script>

And here is my the html:

        <table cellpadding="10">
            <tr>
                <th>Timer</th>
                <th>&nbsp;</th>
            </tr>
            <tr>
                <td class="timer">
                    <span id="days"></span>
                    <span id="hours"></span>
                    <span id="minutes"></span>
                    <span id="seconds"></span>
                </td>
                <td><button type="button" class="btn-start-timer">Start Now</button></td>
            </tr>
            <tr>
                <td class="timer">
                    <span id="days"></span>
                    <span id="hours"></span>
                    <span id="minutes"></span>
                    <span id="seconds"></span>
                </td>
                <td><button type="button" class="btn-start-timer">Start Now</button></td>
            </tr>
        </table>

How can I make the time not to reset once it is started?

  • 写回答

2条回答 默认 最新

  • douqi1931 2013-12-12 22:59
    关注

    Use the event :

    onbeforeunload

    To detect when your user leave your page. Then, send an ajax request. To handle your request, you simply want to save whatever information in a $_SESSION variable in posted.php.

    // posted.php
    session_start();
    if ( isset ( $_POST['hours'] && isset ( $_POST['minutes'] && isset ( $_POST['seconds'] && )
    {
        $_SESSION['hours'] = $_POST['hours'];
        $_SESSION['minutes'] = $_POST['minutes'];
        $_SESSION['seconds'] = $_POST['seconds'];
    }
    

    Then when your new page load / refresh you should have in your javascript your variable that are initialized.

    var hours = <?php if ( isset($_SESSION['hours']) ) echo $_SESSION['hours'] else 0; ?>;
    var minutes = <?php if ( isset($_SESSION['minutes']) ) echo $_SESSION['minutes'] else 0; ?>;
    var seconds = <?php if ( isset($_SESSION['seconds ']) ) echo $_SESSION['seconds '] else 0; ?>;
    

    Now understand that this is a shot in the dark since I have 0 idea about the way your application is built. If you're using some kind of framework it might not be that easy but if you're programming something small or procedural, then this will fit your needs.

    EDIT: I understand that you have multiple timer, instead of using

    $_SESSION['hours']
    

    use

    $_SESSION['idOfTimer']['hours']
    

    Each timer will have his own timer.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行