> From: Nick Fitzsimons
> Finally, by assigning a string to setInterval you are 
> basically performing an "eval", which is very inefficient. 
> Creating a function reference using a closure is the 
> preferred approach.

A function reference is the best way to do it, but not so much because of
efficiency.

If you're just evaling a single function call once every five seconds, you'd
never notice a difference in performance between eval and a function
reference.

For me, the real reason to use a function reference is because it makes the
code cleaner and it's easier to pass data back and forth.

> function joinRoom(roomId){
>     intvarM = window.setInterval((function(roomId) {
>         return function() { // return a function to be used by the timer
>             joinRoomFunction(roomId);
>         }
>     })(roomId), 5000);
> }

Whoa, that's a lot more complicated than it has to be. It's using two
closures where one is enough.

You can write this code instead and it will do exactly the same thing:

  function joinRoom(roomId){
      intvarM = setInterval( function() {
          joinRoomFunction(roomId);
      }, 5000 );
  }

-Mike

Reply via email to