Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 提交表单时停止计时器功能的执行_Javascript_Jquery_Timer_Form Submit - Fatal编程技术网

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

Javascript 提交表单时停止计时器功能的执行,javascript,jquery,timer,form-submit,Javascript,Jquery,Timer,Form Submit,我有一个在线测试程序,用户需要在一段时间内完成它。当用户的时间用完时,我会显示一个警报,说您的时间到了,他会被重定向到结果页面。当用户在时间到期之前完成测验并且在结果页面内时,我会收到相同的警报。我已经修改了如下代码,但它不工作。我在一个名为questions.php的ajax请求页面中调用函数initTimer(1,1) 在index.php中 function initTimer(periodInSeconds, status) { if (status == 0) { retu

我有一个在线测试程序,用户需要在一段时间内完成它。当用户的时间用完时,我会显示一个警报,说您的时间到了,他会被重定向到结果页面。当用户在时间到期之前完成测验并且在结果页面内时,我会收到相同的警报。我已经修改了如下代码,但它不工作。我在一个名为questions.php的ajax请求页面中调用函数initTimer(1,1)

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);
    }
  })
});
函数initTimer(periodInSeconds,状态){
如果(状态==0){
返回false;
}
var end=Date.now()+periodInSeconds*1000*60;
var x=window.setInterval(函数(){
var timeLeft=Math.floor((end-Date.now())/1000);
如果(时间间隔<0){
净间隔(x);
警惕(“时间到了!”);
timeExpired=true;
完成风险值=1;
$.ajax({
类型:“POST”,
url:“success.php”,
数据:{
“用户ID”:
},
成功:功能(hasil){
$('.response_div').html(hasil);
}
});
}
$(document).find(“#timerspan”).html('00:'+(timeLeft<10?'0'+timeLeft:timeLeft));
}, 200);
}
//当用户在时间到期前提交表单时
$(文档).on('submit','form_choice',function(){
初始化计时器(1,0)
$.ajax({
类型:“POST”,
url:“result.php”,
数据:数据,
成功:功能(hasil){
$('.response_div').html(hasil);
}
})
});

我不希望在用户在时间到期之前提交表单时执行init函数()。请帮助我在
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);
    }
  })
});
var定时器;
函数初始化计时器(periodInSeconds,状态){
如果(状态==0){
清除间隔(计时器);
返回;
}
var end=Date.now()+periodInSeconds*1000*60;
计时器=window.setInterval(函数(){
var timeLeft=Math.floor((end-Date.now())/1000);
如果(时间间隔<0){
清除间隔(计时器);
警惕(“时间到了!”);
timeExpired=true;
完成风险值=1;
$.ajax({
类型:“POST”,
url:“success.php”,
数据:{
“用户ID”:
},
成功:功能(hasil){
$('.response_div').html(hasil);
}
});
}
$(document).find(“#timerspan”).html('00:'+(timeLeft<10?'0'+timeLeft:timeLeft));
}, 200);
}
//当用户在时间到期前提交表单时
$(文档).on('submit','form_choice',function(){
初始化计时器(1,0)
$.ajax({
类型:“POST”,
url:“result.php”,
数据:数据,
成功:功能(hasil){
$('.response_div').html(hasil);
}
})
});

现在,即使表单已提交且我处于结果页面,警报仍会显示我无数次