Mamen
Mamen

Reputation: 1436

Stop countdown javascript

i'm new in js and i have created simple countdown but i want to stop it if the value of count = 0 this my script

var no;
no = 5;
$(document).ready(
    function(){
    timer();
    var t = setInterval(timer, 1000);
    var c = setInterval(cek, 1000);
    }

);
function timer(){
      no--;
      $('.timer').html(no);
}

function cek(){
    if(no===0){
        alert('done');     
        clearInterval(t);
        clearInterval(c);


    }

}

Thi is the link of my script : here

Upvotes: 0

Views: 226

Answers (6)

Geo
Geo

Reputation: 49

Try this :

function timer(){
  no--;
    if (no >= 0)
  $('.timer').html(no);
}

Upvotes: 0

You can try doing this (there is no need to create 2 async timers when you can achieve the same with only one such interval):

var t;
var no = 5;
$(document).ready(function() {
    t = setInterval(function() {
        timer();
        cek();
    }, 1000);
});

function timer() {
    no--;
    console.log('NO');
    $('.timer').html(no);
}

function cek() {
    if(no===0) {
        alert('done');     
        clearInterval(t);
    }
}

You can check an updated version of your fiddle @ http://jsfiddle.net/bG8yr/5/

Upvotes: 2

csharpwinphonexaml
csharpwinphonexaml

Reputation: 3683

Here is the answer

http://jsfiddle.net/bG8yr/4/

var no;
no = 5;
var isRunning=false;
$(document).ready(
    function(){ isRunning=true;
    timer();
    var t = setInterval(timer, 1000);
    var c = setInterval(cek, 1000);
    }
);
function timer(){
    if(isRunning){
      no--;
        $('.timer').html(no+":"+isRunning);
    }
}

function cek(){
    if(no===0&&isRunning){
        isRunning=false;
        alert('done');     
        clearInterval(t);
        clearInterval(c);
    }
}

Upvotes: 0

Muhammad Irfan
Muhammad Irfan

Reputation: 228

here is the javascript code which you required. just include one if-else which is very simple. all code is your. just add if timer goes less than 0 than do nothing else coutdown.

var no;
no = 5;
$(document).ready(
    function(){
    timer();
    var t = setInterval(timer, 1000);
    var c = setInterval(cek, 1000);
    }

);
function timer(){
  no--;
    if(no<0){ //nothing }
    else{
      $('.timer').html(no);
    }
}
function cek(){
    if(no===0){   
        alert('done');  
        clearInterval(t);
        clearInterval(c);
    }

}

Upvotes: 0

Jayesh Chandrapal
Jayesh Chandrapal

Reputation: 684

This will do exactly what you want:

var no, 
t;
no = 5;

$(document).ready(

    function () {
        t = setInterval(timer, 1000);
    }

);

function timer() {
    no--;
    $('.timer').html(no);
    if (no === 0) {
        alert('done');
        clearInterval(t);
    }
}

Upvotes: 0

Nix
Nix

Reputation: 58562

Simple issue, your variable was defined in the ready/anonymous function closure. I want to second the fact this is no the best way to do it. In the future inspect the fiddle and look for console errors. Its an easy way to figure out whats going wrong.

http://jsfiddle.net/cYWBL/

var no, t, c, no = 5;
$(document).ready(
    function(){
      timer();
      t = setInterval(timer, 1000);
      c = setInterval(cek, 1000);
    }

);
function timer(){
    no--;
   $('.timer').html(no);
}

function cek(){
    if(no===0){
        alert('done');     
        clearInterval(t);
        clearInterval(c);
    } 
}

Upvotes: 1

Related Questions