Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

javascript - How to get a countdown timer to restart

I am creating an online examination system.

I click the Apply button, then the countdown timer starts. Some time later, the browser page closes. After I click the same exam name, the countdown timer continues from where it left off, but I want the time to start over from the beginning. Please see the screenshots below:

screenshot-1

screenshot-2

View:

    <h2><p style="float: right" id="countdown"></p></h2>

<a href="<?php echo base_url(); ?>student/Examinations/apply_examinations/<?php echo $value['examination_test_id']; ?>" class="btn btn-primary" id="apply" onclick="resetCountdownInLocalStorage();">Apply</a>

    <script>
        $(document).ready(function () {
    $examination_test_id = $("#examination_test_id").val();
            $time_limit = $("#time_limit").val();
            var d = new Date($time_limit);
            var hours = d.getHours();
            var minute = d.getMinutes();
            var minutes = hours * 60 + minute;
            var seconds = 60 * minutes;

            if (typeof (Storage) !== "undefined") {
                if (sessionStorage.seconds) {
                    seconds = sessionStorage.seconds;
                }
            }
            function secondPassed() {
                var hours = Math.floor(seconds / 3600);
                var minutes = Math.floor((seconds - (hours * 3600)) / 60);
                //   var remainingSeconds = seconds - (hours * 3600) - (minutes * 60);
                var remainingSeconds = seconds % 60;

                if (remainingSeconds < 10) {
                    remainingSeconds = "0" + remainingSeconds;
                }
                if (minutes < 10) {
                    minutes = "0" + minutes;
                }

                if (typeof (Storage) !== "undefined") {
                    sessionStorage.setItem("seconds", seconds);
                }

                document.getElementById('countdown').innerHTML = hours + ":" + minutes + ":" + remainingSeconds;

                if (seconds == 0) {
                    clearInterval(myVar);
                    document.getElementById('countdown').innerHTML = alert('Timeout');
                    window.location.href = base_url + "student/Examinations/check_answer/" + $examination_test_id;

                    if (typeof (Storage) !== "undefined") {
                        sessionStorage.removeItem("seconds");
                    }
                } else {
                    seconds--;
                }
            }
            var myVar = setInterval(secondPassed, 1000);
        });

function resetCountdownInLocalStorage() {
        sessionStorage.removeItem("seconds");
    }
    </script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It may depends on browser, according to this link

The lifetime of a browsing context can be unrelated to the lifetime of the actual user agent process itself, as the user agent may support resuming sessions after a restart.

You may have to clean session stroage on tab close I think.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...