Firefox function parameter values not undefined when I'd expect themto be

Discussion in 'Javascript' started by news@chthonic.f9.co.uk, Feb 17, 2010.

  1. 9.co.uk

    9.co.uk Guest

    First off, I KNOW this was a stupid thing to do and I've recoded the
    function I was using to eliminate the parameter entirely, but I just
    spent several hours debugging this and need to share :)

    I have a function that calls itself via setTimeout and is occasionally
    called directly. That function has an optional parameter which I was
    relying on NOT begin set to anything (ie being undefined) when called
    from the setTimeout, and being set to a particular number when called
    directly.

    Of the few browsers I've tested, IE8 and Opera 9.64 gives undefined
    for all calls as you'd expected, but firefox 3.5.7 gives undefined for
    the first call and then zero and the occasional random numbers as the
    parameter value:

    <html>
    <head>
    <title>timeout bug test</title>
    <script type="text/javascript">
    var timerDelay = 1000;
    function doSomething(optionalParameter) {
    var outputElement = document.getElementById('output');
    outputElement.innerHTML += '<br>optionalParameter = '
    + optionalParameter;
    window.setTimeout(doSomething, timerDelay);
    }
    </script>
    </head>
    <body onload="doSomething();">
    <p id="output"></p>
    </body>
    </html>

    The answer, as I've said, is don't do it this way. But the question
    is, is this a bug in Firefox, or a case where the language behaviour
    is not defined.

    I'd appreciate people's thoughts.

    Mike
    --
    Mike Trinder
    www.chthonicionic.net
     
    9.co.uk, Feb 17, 2010
    #1
    1. Advertising

  2. Re: Firefox function parameter values not undefined when I'd expectthem to be

    On Feb 17, 12:38 pm, 9.co.uk wrote:
    <snip>
    > The answer, as I've said, is don't do it this way. But the
    > question is, is this a bug in Firefox, or a case where the
    > language behaviour is not defined.


    Neither, this is not a language issue, but rather a hose environment
    issue. The/any - setTimeout - function is provided by the (browser)
    host and since no applicable standard exists, its behaviour should not
    be expected to be consistent between environments.

    The specific behaviour you describe in Firefox has been in that family
    of browsers since Netscape 4 at least. There are even people who
    maintain that it is the 'correct' behaviour, and charge Microsoft,
    etc. with failing when the do not reproduce it.

    > I'd appreciate people's thoughts.


    Browser scripting is not an area where observations made in any number
    of specific environments can be generalised as being applicable in all
    environments.

    Richard.
     
    Richard Cornford, Feb 17, 2010
    #2
    1. Advertising

  3. 9.co.uk

    9.co.uk Guest

    Re: Firefox function parameter values not undefined when I'd expectthem to be

    On Feb 17, 2:07 pm, Stefan Weiss <> wrote:
    > On 17/02/10 13:38, 9.co.uk wrote:
    >
    >
    >
    > > First off, I KNOW this was a stupid thing to do and I've recoded the
    > > function I was using to eliminate the parameter entirely, but I just
    > > spent several hours debugging this and need to share :)

    >
    > > I have a function that calls itself via setTimeout and is occasionally
    > > called directly. That function has an optional parameter which I was
    > > relying on NOT begin set to anything (ie being undefined) when called
    > > from the setTimeout, and being set to a particular number when called
    > > directly.

    >
    > > Of the few browsers I've tested, IE8 and Opera 9.64 gives undefined
    > > for all calls as you'd expected, but firefox 3.5.7 gives undefined for
    > > the first call and then zero and the occasional random numbers as the
    > > parameter value:

    >
    > > <html>
    > > <head>
    > >   <title>timeout bug test</title>
    > >   <script type="text/javascript">
    > >      var timerDelay = 1000;
    > >        function doSomething(optionalParameter) {
    > >          var outputElement = document.getElementById('output');
    > >          outputElement.innerHTML += '<br>optionalParameter = '
    > >               + optionalParameter;
    > >          window.setTimeout(doSomething, timerDelay);
    > >        }
    > >   </script>
    > > </head>
    > > <body onload="doSomething();">
    > >   <p id="output"></p>
    > > </body>
    > > </html>

    >
    > > The answer, as I've said, is don't do it this way. But the question
    > > is, is this a bug in Firefox, or a case where the language behaviour
    > > is not defined.

    >
    > Actually, it's both. As Richard said, window.setTimeout is not a
    > language feature, and thus its behavior is not defined by the language
    > standard.
    >
    > Your specific observation is a very old bug (or rather oddity) in
    > Mozilla browsers: the extra argument is the "lateness" of the function
    > call.


    Doh! As soon as you mentioned that, I remembered this from way, way
    back. It bit me particularly badly because I was expecting a numeric
    parameter or "undefined" and mozilla was helpfully giving me a numeric
    every time.

    >
    > This bug has been closed as WONTFIX, apparently to maintain
    > compatibility with Netscape 4:
    >
    > https://bugzilla.mozilla.org/show_bug.cgi?id=10637
    >


    I'm sure this will please all the Netscape 4 users out there :)

    Thanks for the comprehensive reply (and thanks too to Richard who got
    in there first with a more general response)

    Mike
     
    9.co.uk, Feb 17, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor
    Replies:
    70
    Views:
    1,766
    Dan Pop
    Feb 17, 2004
  2. Mister B
    Replies:
    8
    Views:
    554
    Nick Keighley
    Aug 26, 2010
  3. Simon Strandgaard

    how to expect eof with expect+pty

    Simon Strandgaard, Dec 20, 2006, in forum: Ruby
    Replies:
    4
    Views:
    358
    Simon Strandgaard
    Dec 20, 2006
  4. Phil
    Replies:
    0
    Views:
    168
  5. AzamSharp
    Replies:
    2
    Views:
    181
Loading...

Share This Page