0

Am working with a count down timer and I want to stop it when is 0, Here's my code

 function countdown() {
    // your code goes here
    var count = $('.c').attr('id');
    var timerId = setInterval(function () {
        count--;
         $('.c').text(count)
        if (count == 0) {
            $("#page").load("page.php");
            $('#beforeloading').html('');
            count = 60;
        }
    }, 1000);
}

 

Phani Kumar M
  • 4,564
  • 1
  • 14
  • 26
Iamgisnet
  • 171
  • 3
  • 3
  • 12

4 Answers4

0

You just need to clearInterval when the count is 0. https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval

jas7457
  • 1,712
  • 13
  • 21
0

You'd clear the interval inside the condition

function countdown() {
  var count = $('.c').attr('id');
  var timerId = setInterval(function() {
    count--;
    $('.c').text(count)
    if (count == 0) {
      $("#page").load("page.php");
      $('#beforeloading').html('');

      clearInterval(timerId); // here
    }
  }, 1000);
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

I think you are looking for clearInterval:

   if (count == 0) {
        $("#page").load("page.php");
        $('#beforeloading').html('');
        count = 60;
        clearInterval(timerId);
    }
0

var Counter=1;
var myInterval= setInterval(function(){
  $("span").text(Counter);
   Counter++;
   if(Counter==11){
     clearInterval(myInterval);
       $("span").append(" Stop");
   }

}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>Stop After 10s</h4><br>
<span></span>
Farhad Bagherlo
  • 6,725
  • 3
  • 25
  • 47