简体   繁体   English

提交表单时停止执行计时器功能

[英]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. 我在一个名为questions.php的ajax请求页面中调用函数initTimer(1,1)。

In index.php 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 我不希望用户在时间到期之前提交表单时执行init函数()。请帮助我

Declare the variable holding the timer outside the initTimer function, then you can clear the timer by calling it with status = 0 initTimer函数外部声明用于保存计时器的变量,然后可以通过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);
    }
  })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM