-1

I have created an online examination system and I have put a countdown timer in online examination system. Below is my javascript code. Plz check

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

<script>
    $(document).ready(function () {
 $examination_test_id = $("#examination_test_id").val();
        $time_limit = $("#time_limit").val();
        var d = new Date($time_limit);  //14-August-2016 01:20:00
        var hours = d.getHours();       //01
        var minute = d.getMinutes();    //20
        var minutes = hours * 60 + minute;
        var seconds = 60 * minutes;     //00
        console.log(seconds);
        if (typeof (Storage) !== "undefined") {      //checks if localStorage is enabled
            if (sessionStorage.seconds) {                       //checks if seconds are saved to localstorage
                seconds = sessionStorage.seconds;

            }
        }
        function secondPassed() {
            var minutes = parseInt((seconds) / 60);
            var hours = parseInt(minutes / 60);
            var remainingSeconds = seconds % 60;
            if (remainingSeconds < 10) {
                remainingSeconds = "0" + remainingSeconds;
            }

            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);

    });
});
</script>

MY Question: coundown timer should start at 01:20:00 ,but in my case, countdown timer starts at 01:80:00 , why? please check my javascript code

coder
  • 906
  • 1
  • 12
  • 19

3 Answers3

0

var minutes = hours * 60 + minute;

One hour and twenty minutes is 80 minutes in total. You should change that line to just have var minutes = minute;

jedifans
  • 2,287
  • 1
  • 13
  • 9
0

In minutes, you are taking the total,

var minutes = hours * 60 + minute;
     80= 1*60 + 20
Ruhul
  • 990
  • 7
  • 15
0

in less than 5 seconds i just found an answer: Javascript return number of days,hours,minutes,seconds between two dates

or JavaScript seconds to time string with format hh:mm:ss

So please just search a little bit before posting this kind of question

Community
  • 1
  • 1
Gatsbill
  • 1,760
  • 1
  • 12
  • 25