0

Im running a recursive setTimeout function and I can run it many times, it has a clearTimeout, with this property I can handle how to stop the function running. But I can't figure out how to stop it in another function.

var a = 0;
var b = 0;
function Listener(x, y) {
    var lValue = y == true ? a : b;
    if (lValue < 100) {
        console.log(lValue);
        if(y == true){
            a+=x; 
        }else{
            b+=x;
        } 
        setTimeout(Listener.bind(this, x, y), 1000);
    } else {
        clearTimeout(Listener);
        if(y == true){
            a=0; 
        }else{
            b=0;
        } 
    }
}

When i tried to run it twice, it works:

enter image description here

My doubt is: How can I stop a particular running instance.

3 Answers 3

3

A couple notes:

  • Given the constant timeout of 1000, you should be using setInterval() instead. It will greatly simplify your function, and allow you to cancel the interval whenever you want.
  • Using global variables is code smell, create an object instead to hold a reference to the value being incremented. Doing so will also allow your function parameters to be more intuitive.

Here's a solution incorporating these two suggestions:

function range (step, start = 0, stop = 100) {
  const state = {
    value: start,
    interval: setInterval(callback, 1000)
  };
  
  function callback () {
    if (state.value < stop) {
      console.log(state.value);
      state.value += step;
    } else {
      console.log('timer done');
      clearInterval(state.interval);
    }
  }

  callback();

  return state;
}

const timer1 = range(10);
const timer2 = range(20);

setTimeout(() => {
  console.log('stopping timer 1');
  clearInterval(timer1.interval);
}, 2500);

setTimeout(() => {
  console.log('timer 2 value:', timer2.value);
}, 3500);

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

Comments

1

You could elevate the timer to a higher scope that is accessible by other functions.

var a = 0;
var b = 0;
var timer = null;

function Listener(x, y) {
  var lValue = y == true ? a : b;
  if (lValue < 100) {
    console.log(lValue);
    if (y == true) {
      a += x;
    } else {
      b += x;
    }
    timer = setTimeout(Listener.bind(this, x, y), 1000);
  } else {
    clearTimeout(timer);
    if (y == true) {
      a = 0;
    } else {
      b = 0;
    }
  }
}

function clearTimer() {
  if (timer !== null) clearTimeout(timer);
}

Listener(3, 3);
// clear the timer after 3 secnods
setTimeout(clearTimer, 3000);

Comments

0

create a variable and store the reference of setTimout on that variable, after that you just clearTimout with that variable reference

e.x

GLOBAL VARIABLE:

var k = setTimeout(() => { alert(1)}, 10000)

clearTimeout(k)





 var a = 0;
    var b = 0;
    var recursiveFunctionTimeout = null;

    function Listener(x, y) {
        var lValue = y == true ? a : b;

        if (lValue < 100) {
            console.log(lValue);
            if(y == true){
                a+=x; 
            }else{
                b+=x;
            } 
            recursiveFunctionTimeout = setTimeout(Listener.bind(this, x, y), 10);
        } else {
            clearTimeout(recursiveFunctionTimeout);

            if(y == true){
                a=0; 
            }else{
                b=0;
            } 
        }
    }

LOCAL VARIABLE:

var a = 0;
var b = 0;

function Listener(x, y) {
    var lValue = y == true ? a : b;
    var recursiveFunctionTimeout = null;

    if (lValue < 100) {
        console.log(lValue);
        if(y == true){
            a+=x; 
        }else{
            b+=x;
        } 
        recursiveFunctionTimeout = setTimeout(Listener.bind(this, x, y), 10);
    } else {
        clearTimeout(recursiveFunctionTimeout);

        if(y == true){
            a=0; 
        }else{
            b=0;
        } 
    }
}

2 Comments

How can I pass the parameters with your example??
You can do that as a global variable, or as a local variable inside

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.