0

I have a function that displays a count down clock according to a variable. What would be the right way to stop it in the specified condition? clearTimeout is not stoping the Decrement functions.

function interface_vip(type){
    var timeout = '';
    var clock = '';
    //display clock
    function Decrement() {
        currentMinutes = Math.floor(secs / 60);
        currentSeconds = secs % 60;
        if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
            secs--;
            if(secs !== -1) timeout = setTimeout(Decrement,1000);
    }
    if (type == 1){
        var mins = 20;
        var secs = mins * 60;
        var currentSeconds = 0;
        var currentMinutes = 0;
        clock = setTimeout(Decrement,1000);
    }
    if (type == 2){
        clearTimeout(clock);
        clearTimeout(timeout);
    }
}
5
  • you have an error, t is not defined Commented Feb 9, 2017 at 9:23
  • 1
    look that interface is a reserved word in JavaScript mathiasbynens.be/notes/reserved-keywords#ecmascript-2 Commented Feb 9, 2017 at 9:24
  • variable t is defined in my js, I'll changed it to a number in my question, it's working Commented Feb 9, 2017 at 9:25
  • @Lionel T right, changed in my question I used this word translated in the original code and did not realize it when did the question. Commented Feb 9, 2017 at 9:26
  • great, for the rest I don't understand at all the question, could you post a working (or at least trying to) snippet/fiddle/pen? Commented Feb 9, 2017 at 9:36

1 Answer 1

2

Your clock id is lost at the second call ,a poor solution is to create the variables global

    var timeout = '';
        var clock = '';
  function interface_vip(type){   
        //display clock
        function Decrement() {
            currentMinutes = Math.floor(secs / 60);
            currentSeconds = secs % 60;
            if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
                secs--;
                if(secs !== -1) timeout = setTimeout(Decrement,1000);
        }
        if (type == 1){
            var mins = 20;
            var secs = mins * 60;
            var currentSeconds = 0;
            var currentMinutes = 0;
            clock = setTimeout(Decrement,1000);
        }
        if (type == 2){
            clearTimeout(clock);
            clearTimeout(timeout);
        }
    }

a better approach in the bellow snippet

function interface_vip(){
    var timeout = '';
    var t = 0;
    var clock = '';
    //display clock
    function Decrement() {
        currentMinutes = Math.floor(secs / 60);
        currentSeconds = secs % 60;
        if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
            secs--;
            if(secs !== -1) timeout = setTimeout(Decrement,1000);
    }
   this.start = function(){
        var mins = t;
        var secs = mins * 60;
        var currentSeconds = 0;
        var currentMinutes = 0;
        clock = setTimeout(Decrement,1000);
    }
    this.stop = function(){
        clearTimeout(clock);
        clearTimeout(timeout);
    }
}
var interf = new interface_vip();
interf.start();
interf.stop();

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

2 Comments

Thank you! I couldn't see it. That solves the problem :D
I've added a better solution in the answer

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.