Search code examples
javascriptjquerytimerform-submit

stop the timer function from executing when form is submitted


I have an online quiz program where user needs to complete it within a time period. When user runs out of time,I show an alert saying that your time is up and he is redirected to result page. I get the same alert when user completes the quiz before the time expires and is inside result page. I have modified the code like following but its not working. I am calling the function initTimer(1,1) inside an ajax requested page named questions.php.

In index.php

function initTimer(periodInSeconds, status) {
  if (status == 0) {
    return false;
  }
  var end = Date.now() + periodInSeconds * 1000 * 60;
  var x = window.setInterval(function() {
    var timeLeft = Math.floor((end - Date.now()) / 1000);

    if (timeLeft < 0) {
      clearInterval(x);
      alert("Time's Up!");
      timeExpired = true;
      var completed = 1;
      $.ajax({
        type: "POST",
        url: "success.php",
        data: {
          'userID': <?php echo $_SESSION['userID'];?>
        },
        success: function(hasil) {
          $('.response_div').html(hasil);
        }
      });
    }

    $(document).find('#timerspan').html('00:' + (timeLeft < 10 ? '0' + timeLeft : timeLeft));
  }, 200);
}
//when user submits the form before time expires

$(document).on('submit', '.form_choice', function() {
  initTimer(1, 0)
  $.ajax({
    type: "POST",
    url: "result.php",
    data: data,
    success: function(hasil) {
      $('.response_div').html(hasil);
    }
  })
});

I dont want the init function() to execute when user submits the form before time expires.Please help me


Solution

  • Declare the variable holding the timer outside the initTimer function, then you can clear the timer by calling it with status = 0

    var timer;
    
    function initTimer(periodInSeconds, status) {
      if (status == 0) {
        clearInterval(timer);
        return;
      }
      var end = Date.now() + periodInSeconds * 1000 * 60;
      timer = window.setInterval(function() {
        var timeLeft = Math.floor((end - Date.now()) / 1000);
    
        if (timeLeft < 0) {
          clearInterval(timer);
          alert("Time's Up!");
          timeExpired = true;
          var completed = 1;
          $.ajax({
            type: "POST",
            url: "success.php",
            data: {
              'userID': <?php echo $_SESSION['userID'];?>
            },
            success: function(hasil) {
              $('.response_div').html(hasil);
            }
          });
        }
    
        $(document).find('#timerspan').html('00:' + (timeLeft < 10 ? '0' + timeLeft : timeLeft));
      }, 200);
    }
    //when user submits the form before time expires
    
    $(document).on('submit', '.form_choice', function() {
      initTimer(1, 0)
      $.ajax({
        type: "POST",
        url: "result.php",
        data: data,
        success: function(hasil) {
          $('.response_div').html(hasil);
        }
      })
    });