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



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

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:

<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);
<body onload="doSomething();">
<p id="output"></p>

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.


Richard Cornford

On Feb 17, 12:38 pm, (e-mail address removed) wrote:
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



Actually, it's both. As Richard said, window.setTimeout is not a
language feature, and thus its behavior is not defined by the language

Your specific observation is a very old bug (or rather oddity) in
Mozilla browsers: the extra argument is the "lateness" of the function

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:

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)


Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Latest member

Latest Threads