0

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

6 Answers 6

2

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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);
    } 
}

Comments

0

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);
    }
}

Comments

0

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);
    }

}

Comments

0

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);
    }
}

Comments

0

Try this :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.