重新初始化jquery倒计时器

时间:2016-05-23 17:59:54

标签: javascript jquery html timer

我正在创建一个考试页面,其中每个问题使用倒计时器。根据按钮点击重新初始化计时器,从数据库表中获取下一个问题的时间,我使用下面的代码进行倒数计时器,但是问题是每次我想重新初始化它都不能正常工作,有时它会在某些时候变得越来越快而无法工作......

<label style="text-align:center;">Question Time : <span id="time"></span></label>

var ttime = 0;
function loadQuestion(id){
        $.ajax({
            url: "Data.php",
            method:"POST",
            data:{q_id:id},
            context: document.body,
            success: function(response){
                alert(response);
                var obj = JSON.parse(response);
                ttime = obj.ques_time;
         });
}   

    function startTimer(duration, display) {
        var timer = duration, minutes, seconds;
        setInterval(function () {
            minutes = parseInt(timer / 60, 10);
            seconds = parseInt(timer % 60, 10);

            minutes = minutes < 10 ? "0" + minutes : minutes;
            seconds = seconds < 10 ? "0" + seconds : seconds;

            display.text(minutes + ":" + seconds);

            if (--timer < 0) {
                //timer = duration;

                //do something
            }
        }, 1000);
    }

    jQuery(function ($) {
        var fiveMinutes = 60 * ttime,
            display = $('#time');
        startTimer(fiveMinutes, display);
    });

最后一件事我需要提一下,当时间达到0时,我想做一些任务。

1 个答案:

答案 0 :(得分:0)

将计时器保存在变量中:

var timer = setInterval(function(){}, interval);

然后做:

clearInterval(timer);

使用clearInterval或clearTimeout函数禁用间隔,然后可以重置它。 要做到这一点,您需要做的就是设置一个新的计时器。

此外,setInterval()函数的interval参数为毫秒,因此1000表示该函数每秒都会发生。

所以,如果你想倒数五分钟:

&#13;
&#13;
var minutes = 5;
var seconds = 0;

var timer; // Will hold the timer 



// Start the timer
$('#startBtn').click(function(){
  clearInterval(timer); // Just incase 
  
  timer = setInterval(function(){
    
      // This function will occur every second 
      if (seconds > 0)
      { 
          seconds--;
      } 
      else if (minutes > 0) 
      { 
          seconds = 59; 
          minutes--;
      } 
      else 
      {
        $('#time').html("Your time is up!");
      }  

      // Show the time left
      $('#time').html(minutes + ":" + seconds + " remaining");
 }, 1000);
});


// Reset the timer
$('#resetBtn').click(function(){
  
  // Clear the interval
  clearInterval(timer);
  
  // Reset time 
  minutes = 5;
  seconds = 0;

  // Show the time left
  $('#time').html(minutes + ":" + seconds + " remaining");
});




function countDown(){
// This function will occur every second 
  if (seconds > 0)
  { 
    seconds--;
  } 
  else if (minutes > 0) 
  { 
    seconds = 59; 
    minutes--;
  } 
  else 
  {
    $('#time').innerHTML = "Your time is up!";
  } 

  // Show the time left
  $('#time').innerHTML = minutes + ":" + seconds + " remaining";
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="time">5:00 remaining</div><br/>

<div id="startBtn">Click me to start!</div>
<div id="resetBtn">Click me to reset!</div>
&#13;
&#13;
&#13;