Monday, April 12, 2010

setTimeout() fires function twice? Firefox Bug?

After 7+ hours of listening to Crockford on JavaScript and some other lectures, I've decided to write my own JS Framework for a current project. As I'm working through various bits I discovered what appears to be a bug in Firefox 3.6.

This fires twice, unexpectedly:
function all(){
            var promise = make_promise();
            // Simulate async call from some source
            setTimeout(function(){
              promise.fulfill(fixture);
            }, 3000);
            return promise;
          }

This fires just once, as expected:
function all(){
            var promise = make_promise();
            // Simulate async call from some source
            function fulfill(){
              promise.fulfill(fixture);
            }
            setTimeout(fulfill, 3000);
            return promise;
          }

2 Comments:

Blogger Jacques Petit said...

I'm seeing a smilar behavior but your solution doesn't work in my tests.

Attempting to use global functions didn't work either for me.

April 20, 2010 at 2:54 PM  
Blogger CoolAJ86 said...

There are other work arounds.

I suggest using Doug Crockfords "once" on the function - if you in fact only need to use it once.
http://gist.github.com/373144

If the issue is that the function shouldn't be called twice before the first finishes, you can adapt it.

Feel free to pop in #ujsug (IRC) or send a message to our google group if you want to discuss the issue.

April 20, 2010 at 3:13 PM  

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home