setTimeout different behaviour in Firefox and IE

Discussion in 'Javascript' started by alex.malgaroli@gmail.com, Aug 21, 2007.

  1. Guest

    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.
    , Aug 21, 2007
    #1
    1. Advertising

  2. RobG Guest

    On Aug 21, 7:49 pm, ""
    <> wrote:
    > 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.


    --
    Rob
    RobG, Aug 21, 2007
    #2
    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. Andy Chambers
    Replies:
    1
    Views:
    384
    Daniel Dyer
    May 14, 2007
  2. Richard Maher
    Replies:
    4
    Views:
    1,074
    Richard Maher
    May 19, 2009
  3. Replies:
    2
    Views:
    145
    Laurent Bugnion
    Aug 22, 2006
  4. setTimeout and Firefox...

    , Jan 3, 2008, in forum: Javascript
    Replies:
    3
    Views:
    106
    Dr J R Stockton
    Jan 5, 2008
  5. Richard Maher

    FireFox II setTimeout() Go Back and Applets

    Richard Maher, May 15, 2009, in forum: Javascript
    Replies:
    4
    Views:
    151
    Richard Maher
    May 19, 2009
Loading...

Share This Page