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

N

news

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
 
R

Richard Cornford

On Feb 17, 12:38 pm, (e-mail address removed)9.co.uk 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
environments.

Richard.
 
N

news

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top