Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - setInterval()

I am making a game using Javascript, and to update the game I am using setInterval(). Should I use clearInterval() somehow on the window exit, or does it automatically stop?

like image 858
MCMastery Avatar asked May 10 '15 23:05

MCMastery


People also ask

What does setInterval () method do in JS?

The setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed.

Is it good to use setInterval in JavaScript?

In order to understand why setInterval is evil we need to keep in mind a fact that javascript is essentially single threaded, meaning it will not perform more than one operation at a time.

How do you do intervals in JavaScript?

There are two methods for executing code at specific intervals. They are: setInterval() setTimeout()

What is the difference between setTimeout and setInterval in JavaScript?

setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.


1 Answers

The browser is (or better: should be) clever enough to fully clean up a closed website. That includes aborting it's running connections and terminating scripts. Data that stays on your PC after you close the tab usually only includes stuff like browser history entries, cookies, local storage and cached data (e.g. scripts, stylesheets, images).

like image 198
Felk Avatar answered Oct 11 '22 08:10

Felk