setTimeout different behaviour in Firefox and IE

A

alex.malgaroli

Hi all.
I didn't find anything on the web regarding this different behaviour I
found in Firefox (v. 2) and IE (v. 6), so I'm posting it here, maybe
someone will benefit from this.

I have a function in a javascript object like this:

function MyObject.prototype = {
myvar = "This is myvar";
myfunction(myarg) {
// do something with myarg
}

}

If I issue a setTimeout call using a function with some additional
arguments in this form:

setTimeout(this.myfunction, 5, this.myvar);

(for example in another MyObject method), Firefox calls myfunction
passing myvar correctly (as a string valued "This is myvar"). Internet
Explorer seems to pass myvar as undefined.

My idea is that FF evaluates immediately the value to pass to the
function, stores it, and then when the time comes, invokes the
function with the correct value.
Instead it seems (but this is my guess) that IE evaluates the argument
only when it calls the function, thus having lost in the scope the
reference to "this".

I don't have tried it but I think that if I pass global variables or
"local" variables to the setTimeout additional arguments, they work...

Well... I just found this two different doc for setTimeout. I have a
JS reference in PDF (a bit old, referring to 1.3 version) that
complies with the second. This may be the reason why I get
undefined...
http://msdn2.microsoft.com/en-us/library/ms536753.aspx
http://developer.mozilla.org/en/docs/DOM:window.setTimeout#1021427

Just for the sake of completeness: I was using MochiKit so i resolved
using a partial(), when invoking setTimeout thus binding at setTimeout
invoke time the argument values. Worked fine.
 
R

RobG

Hi all.
I didn't find anything on the web regarding this different behaviour I
found in Firefox (v. 2) and IE (v. 6), so I'm posting it here, maybe
someone will benefit from this.

I have a function in a javascript object like this:

function MyObject.prototype = {

That is a plain syntax error. The rest of your code is similarly
plagued with errors. If you post code, it should be able to be copied
and pasted into a page and exhibit the behaviour you describe.

myvar = "This is myvar";

If that line is ever is ever executed (in your example, it isn't) it
will create a global property myvar with a value of "This is myvar".
myfunction(myarg) {
// do something with myarg
}

More syntax errors...
}

If I issue a setTimeout call using a function with some additional
arguments in this form:

setTimeout(this.myfunction, 5, this.myvar);

I guess this is your actual question. The answer is here:

<URL: http://developer.mozilla.org/en/docs/DOM:window.setTimeout >

Gecko-based browsers (such as Firefox) accept additional parameters to
setTimeout, IE (and probably most other browsers) don't.
 

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

Similar Threads

setTimeout 10
setTimeout() in IE 2
setTimeout and Firefox... 3
onbeforeunload, settimeout and IE 0
SetTimeout not working with IE 4
setTimeout in Opera Mini 4.2? 10
settimeout() in IE 3
setTimeout not working on IE 2

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top