-1

I'm trying to make a timer that will be auto-submit all the answers when times up. But now, it just shows that the time is expired and it is not redirecting to the final score page.My answers are stored in the database, i dont think it is related. Here is the code :

  $(document).ready(function() {
        var endTime = localStorage.getItem("endTime");
        console.log(endTime);
        
        if (endTime != 'underfined' && endTime != null) {
            startCountdownTimer();
            //localStorage.removeItem("endTime");
        }
        
    });

    function setEndTime() {
        var currentTime = new Date().getTime();
        var endTime = new Date(currentTime + 1*60000);
        localStorage.setItem("endTime", endTime);
        // alert('Set end time ' + endTime);

        startCountdownTimer();
    }

    function startCountdownTimer() {
        // Set the date we're counting down to
        var endTime = localStorage.getItem("endTime");
        var countDownDate = new Date(endTime).getTime();

        // Update the count down every 1 second
        var x = setInterval(function() {

            // Get today's date and time
            var now = new Date().getTime();

            // Find the distance between now and the count down date
            var distance = countDownDate - now;

            // Time calculations for days, hours, minutes and seconds
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));
            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((distance % (1000 * 60)) / 1000);

            // Display the result in the element with id="demo"
            document.getElementById("demo").innerHTML = days + "d " + hours + "h "
            + minutes + "m " + seconds + "s ";

            // If the count down is finished,[ I want to make it is redirecting to score page ]
            if (distance <= 0) {
                clearInterval(x);
                setTimeout("document.quiz.submit()",1);
                window.location = "http://localhost/quizzer%202/final.php"
            }
                
        },1000);
    }
qyioma
  • 1
  • 2
  • Your test is very easy to hack, because your timer is on the client side. – code May 10 '21 at 01:51
  • what should i do? – qyioma May 10 '21 at 01:56
  • You are using PHP for server side, correct? – code May 10 '21 at 01:58
  • yes, I'm using PHP for the server side. – qyioma May 10 '21 at 02:05
  • You can still set the timer on the client side and stuff, just make sure that after (for example, your form lasts 10 minutes), that the PHP will stop accepting that form after 10 minutes, so even if a person tampers with your client side script, they'll just be tricking themselves and it'll do nothing. You can use Date manipulation on PHP. – code May 10 '21 at 02:09
  • I'm trying to use another code for the timer, but the problem is when the other user tries to enter the test, the time will continue counting from the last user stop the test. I write timer code(javascript) in PHP. As long as I know, I cannot make onclick button right? to redirecting PHP with javascript code in PHP? For now, I don't have any idea to make the time reset. – qyioma May 10 '21 at 02:22

1 Answers1

0

Here's a solution with JavaScript's native setTimeout function to auto-submit your form:

// The following ($function() {}) is equivalent to $(document).ready(function() {.
$(function() {
   setTimeout(function() {
      setTimeout(function() {$('#idOfYourForm').submit()},1);
      window.location = "http://localhost/quizzer%202/final.php"
   }, 1000 * 60 * 10); // This is the amount of milliseconds your test will last. I set this to ten minutes
});

Also, it doesn't look like the submit command is correct. Since you're using jQuery, I'll show you the jQuery way.

  1. Make an ID for your form
  2. Submit it with $('#idOfYourForm').submit() (one problem was that you put quotation marks around it)

One last thing, when you submit a form, the default action is that you will be redirected, so your last redirect won't do anything. For what you did to work, consider using AJAX. See this post.

code
  • 354
  • 2
  • 12