cookya
cookya

Reputation: 3297

Correct handling of setTimeout in Angular

I have a one-page web application, and in the main component's ngOnInit() function, it calls a function loopDoSomething() which is recursive, using setTimeout, like this:

ngOnInit(): void {
  // Some operations
  this.loopDoSomething();
}

loopDoSomething() {
  // Some operations
  setTimeout(this.loopDoSomething.bind(this), 1500);
}

Should I add a stop condition to loopDoSomething or is that enough?

Basically what I'm asking is whether closing the page will automatically stop the process running loopDoSomething(), or should I handle it manually, in the ngOnDestroy() function?

Upvotes: 0

Views: 1138

Answers (1)

Milad
Milad

Reputation: 28592

Browser itself is the compiler of your Javascript code and when you open a tab, it'll create something called DOM( Document Object Model) of your html document and a global object called window.

When you close your browser, all of these are of course destroyed because the browser itself is the host and host is gone.

Upvotes: 1

Related Questions