views:

191

answers:

1

I first encountered a problem with safari, where set interval would behave unpredicatbly when the function name was not enclosed within quotations (and optionally it seams with added parentheses):

repeatInterval = setInterval("foo()", 50);

Upon changing my code to read in this way, it seams it does not get executed at all in the Mac version of Firefox.

I did some further testing an ensured that it works completely fine under linux and windows versions (including both 3.0.10 and 3.6 under windows). The only combination that throws up this problem is Firefox (3.6 in this case) on Mac OS X Snow Leopard.

It dose not work at all unless written in the following format:

repeatInterval = setInterval(foo, 50);

Is there a sollution to this problem that will work in all other browsers and Firefox on the Mac, without testing for the operating system and browser in the javascript and hacking it to work accordingly?

+1  A: 

Don't pass a string as the first parameter of setInterval or setTimeout. You should either pass a function identifier (as you did with setInterval(foo, 50);) or pass an anonymous function (using the function keyword).

Matt Ball
It seams that all browsers can indeed handle this method: `setInterval(foo, 50)` a problem elsewhere was skewing my understanding of the source of my particular problem. I assumed that `setInterval` acted like an object, and would be overwritten if its only reference `repeatInterval` was overwritten with a new `setInterval`. I solved this by running `clearInterval(repeatInterval)` before assigning `repeatInterval` a new interval.
Marcus Whybrow
`setInterval` is a function that returns a handle - which you really only use for canceling that interval later. It sounds like you understand the usage now.
Matt Ball
Thanks, in any case it helps to hear someone elses opinion to get a feal for which parts of the documenation are more widely.
Marcus Whybrow