Home > Software design >  How to force stop an object implementing recursion in Node.js?
How to force stop an object implementing recursion in Node.js?

Time:11-21

I have a class which does some very stuffy recursion. My goal is that when user pushes a button the recursion will end. I tried understanding the execution context and the stack but then I was thinking about this aproach, just to destroy or clear the object itself. Any ideas?

class Foo {
  computeHeavyRecursion() {

    //some calculus here  
    setTimeout(function() {  
    }, 50000);

  }

  //destroy or clear the object itself
  destroy() {
    //this = null;
  }

}

let obj = new Foo();
obj.computeHeavyRecursion();

setTimeout(function() {
    obj.destroy();
}, 10000);

CodePudding user response:

You can create boolean variable in this class and check in your function this variable. If variable true program call function again else program stops calling.

If you want to destroy setTimeout or setInterval you can use clearTimeout or clearInterval function.

Reference:

  • Related