Search code examples
jqueryjquery-pluginspluginscountdown

Jquery - Reset countdown


I got this plugin from this site http://davidwalsh.name/jquery-countdown-plugin and I manage to run the countdown but I am having problem to stop the countdown when I click a button and reset it. Saw some article about it but can't really relate it to my case. If I call the function again, it will display 4(old counter) --> 9 (new counter) -->3 (old counter) --> 8 (new counter)... together which is messed up. All I need is to stop to reset the counter when I click a button.

countDown code

function countDown(){
  $('#countdown').countDown({
    startNumber:10,
    callBack: function(me) {
      $(me).text('Next!').css('color','#090');        
      if(quesNo < lenLevel ){
        //database transaction
      }else {
        alert('finish! You got correct: '+ correct);
      }
    }
  }); 
}

Solution

  • In your button handler you will need to "stop" the original count down before starting the new one. The following one line addition to the countDown function should solve your problem.

    function countDown(){
      $('#countdown').stop();
      $('#countdown').countDown({ ...