Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

clearTimeout doesn't work when the timeout function is calling itself

I am using the code for accurate timer from this post: https://stackoverflow.com/a/29972322/17490329

but I modified it to be a countdown timer.

I want that when the countdown ends, it will stop, but using clearTimeout in my case doesn’t work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let duration = 5; // seconds
countdownTimer(duration);

function countdownTimer (duration) {
    let interval = 1000; // milliseconds 
    let expected = Date.now() + interval;
    let timeoutHandler;

    setTimeout(step, interval);
    function step() {
        let dt = Date.now() - expected;
        if (dt > interval) {

        }
        console.log(duration);

        expected += interval;

        duration--;
        if (duration <= 0) {            
            clearTimeout(timeoutHandler); // doesn't stop the timer
        }
        timeoutHandler = setTimeout(step, Math.max(0, interval - dt)); // take into account drift
    }
}

Why?

>Solution :

let duration = 5; // seconds
countdownTimer(duration);

function countdownTimer (duration) {
    let interval = 1000; // milliseconds 
    let expected = Date.now() + interval;
    let timeoutHandler;

    setTimeout(step, interval);
    function step() {
        let dt = Date.now() - expected;
        if (dt > interval) {

        }
        console.log(duration);

        expected += interval;

        duration--;
        if (duration <= 0) {            
            clearTimeout(timeoutHandler); // doesn't stop the timer
            return; //don't forget to stop executing function
        }
        timeoutHandler = setTimeout(step, Math.max(0, interval - dt)); // take into account drift
    }
}

inside of "if" condition you miss "return" to stop executing fucntion

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading