Intervals and timeouts
In our game we used a lot of setInterval
calls. You may think that those calls are multithreaded, but they are not. JavaScript is strictly single-threaded (with the recent exception of WebWorkers, but we won't look into that here). This means that all those calls are really run one after the other.
If you're interested in the dirty details of how exactly intervals and timeouts work, I would recommend reading the excellent article written by John Resig, How JavaScript Timers Work (http://ejohn.org/blog/how-javascript-timers-work/).
Therefore, intervals and timeouts don't add multithreading to your code, and there are many reasons why you may want to avoid using them too much. First, it makes your code somewhat difficult to debug. Indeed, depending on how much time each call takes, your interval will be executed in a different order, and even those will be of the exact same periodicity.
Furthermore, performance-wise, using setInterval
and setTimeout
too much can be very...