setTimeout not executing form submit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mic

    #1

    setTimeout not executing form submit

    Goal: delay execution of form submit

    Code (Javascript + JScript ASP):

    <%
    Response.Write( "<OBJECT ID='IntraLaunch ' STYLE='display : none'
    WIDTH=0 HEIGHT=0 CLASSID='CLSID: 0AE533FE-B805-4FD6-8AE1-A619FBEE7A23'
    CODEBASE='Intra Launch.CAB#vers ion=5,0,0,3'>")
    Response.Write( "<PARAM NAME='ImageLoc' VALUE='Null'>")
    Response.Write( "<PARAM NAME='ImageSrc' VALUE='Null'>")
    Response.Write( "<PARAM NAME='Run'
    VALUE='F:\\Axys 3\\Clarity\\rep ort1.bat'>")
    Response.Write( "<PARAM NAME='RunParms' VALUE=''>")
    Response.Write( "<PARAM NAME='Display' VALUE=MAX'>")
    Response.Write( "</OBJECT>")
    %>

    <script>

    function UpdateRec(theFo rm, rec) {

    tmp_checked = 0;

    tmp_login = theForm.login.v alue;
    tmp_year = theForm.tmp_yea r.value;
    tmp_quarter = theForm.tmp_qua rter.value;
    tmp_comm = theForm.comm.va lue;

    if(theForm[rec].checked) {
    tmp_checked = 1;
    }

    theForm.target = "MyFrame";
    theForm.action = "qr_update.asp? axys=" + rec + "&tmp_quart er=" +
    tmp_quarter + "&tmp_year= " + tmp_year + "&tmp_login =" + tmp_login +
    "&tmp_check ed=" + tmp_checked + "&tmp_comm= " + tmp_comm;
    theForm.submit( );
    document.body.s tyle.cursor = 'wait';
    }

    function GenerateReport( theForm, rec, do_checkbox) {

    document.IntraL aunch.RunParms = rec;
    IntraLaunch.Exe cuteIt("Null");

    if (do_checkbox) {
    oldcomm = theForm.comm.va lue;
    theForm.comm.va lue = "GSHOW";
    theForm[rec].checked = true;
    UpdateRec(theFo rm, rec);
    theForm.comm.va lue = oldcomm;
    }

    tmp_year = theForm.tmp_yea r.value;
    tmp_quarter = theForm.tmp_qua rter.value;
    theForm.target = "MyOtherFra me";
    theForm.action = "generate.asp?q uarter=" + tmp_quarter + "&year=" +
    tmp_year + "&axys=" + rec;

    var timer = setTimeout("doc ument."+theForm .name+".submit( )", 2000);
    clearTimeout(ti mer);

    //alert("Generate Report form object name: " + theForm.name);
    //document.update form.submit();
    }

    background/explanation:

    This is a task-status tracking web application. The purpose of this
    code snippet is to automatically create two Axys reports as text
    files, parse them into PDFs, and collate them into a third PDF, all on
    the click of a button. GenerateReport is the method that's called
    directly; the object declaration and UpdateRec are there for your
    reference. The issue here is that it takes some time (a second or
    two) to generate the text files via IntraLaunch, and I need to delay
    the PDF operations (performed in generate.asp) until this is done.

    The code itself is sound -- by uncommenting the last two lines,
    everything functions smoothly, since the alert acts as a delay.

    I have found that setTimeout is not executing the form submit at all:
    I placed alerts in <body onload=> of both qr_update.asp and
    generate.asp. The sans-setTimeout version pops an alert for each page,
    but if I use setTimeout, I only get an alert for qr_update. I am
    puzzled, though, because this seems to be the correct syntax for form
    submission via setTimeout:


    Please help!

    Thanks,
    Michelle
  • Lee

    #2
    Re: setTimeout not executing form submit

    Mic said:
    [color=blue]
    > var timer = setTimeout("doc ument."+theForm .name+".submit( )", 2000);
    > clearTimeout(ti mer);[/color]

    setTimeout doesn't insert a delay.
    It schedules some task to be performed after a specified delay.
    Execution proceeds immediately to the next line.

    Those two lines say:
    1. Wait two seconds, and then submit the form.
    2. Nevermind.

    Comment

    • Mic

      #3
      Re: setTimeout not executing form submit

      Yep, that fixed it.

      1) I feel retarded
      2) You're awesome -- thanks!!!!!

      :-)

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: setTimeout not executing form submit

        Lee wrote:
        [color=blue]
        > Mic said:[color=green]
        >> var timer = setTimeout("doc ument."+theForm .name+".submit( )", 2000);
        >> clearTimeout(ti mer);[/color]
        >
        > setTimeout doesn't insert a delay.
        > It schedules some task to be performed after a specified delay.[/color]

        Actually, if provided a string as first argument, setTimeout() schedules
        that expression to be evaluated after a specified delay. If provided a
        Function object reference instead, the function referred to is called,
        using the third and following arguments of setTimeout() as arguments for
        the function [JavaScript 1.2+/Gecko-DOM only; the IE-DOM does support
        function references from v5.0 on, but not additional arguments to date
        (IE 6.0 SP-1) -- setTimeout(aler t, 2000, 42) yields an empty message box
        in IE, but not in NN4+/Mozilla.]

        However, the used expression is suboptimal as document.FormNa me is not a
        standards compliant reference (i.e. not specified in W3C-DOM Level 1+).
        Instead document.forms["FormName"] is specified there, thus the better
        statement is either

        var timer =
        setTimeout("doc ument.forms['"+theForm.name +"'].submit()", 2000);

        or

        var timer =
        setTimeout(docu ment.forms[theForm.name].submit, 2000);

        Yet it would be much better to check (calling a user-defined method) if
        there is document.forms[theForm.name].submit() before accessing it. For
        the first variant, as used in a timeout, the pure, unchecked statement
        is likely to fail if the window's content has changed in the meantime for
        some reason. For the second variant, a part of the lookup path may be a
        non-reference anyway, already causing a script error on assignment.
        Therefore one should use

        function submitForm(sFor mName)
        {
        var f, t;
        if (document
        && document.forms
        && (f = document.forms[sFormName])
        && ((t = typeof f.submit) == "function"
        || t == "object")
        {
        f.submit();
        }
        }

        var timer = setTimeout("sub mitForm(" + theForm.name + ")", 2000);


        PointedEars

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: setTimeout not executing form submit

          Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
          [color=blue]
          > var timer =
          > setTimeout(docu ment.forms[theForm.name].submit, 2000);[/color]

          Does this work consistently? I can imagine implementations where
          the submit function is the same for all forms, and relies on the
          "this" reference to find out which form to submit. That would break
          here because the function is called with the global object as "this".

          I would write
          setTimeout(func tion(){document .forms[theForm.name].submit();},200 0);
          to be safe, but I don't know if there is a browser where the former
          version would break.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: setTimeout not executing form submit

            Lasse Reichstein Nielsen wrote:
            [color=blue]
            > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
            >> var timer =
            >> setTimeout(docu ment.forms[theForm.name].submit, 2000);[/color]
            >
            > Does this work consistently? I can imagine implementations where
            > the submit function is the same for all forms, and relies on the
            > "this" reference to find out which form to submit. That would break
            > here because the function is called with the global object as "this".
            >
            > I would write
            > setTimeout(func tion(){document .forms[theForm.name].submit();},200 0);
            > to be safe, but I don't know if there is a browser where the former
            > version would break.[/color]

            Sorry, I don't see why `this' in a submit() method called from
            a global wrapper function is any different from the direct call.


            PointedEars

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: setTimeout not executing form submit

              Lasse Reichstein Nielsen wrote:
              [color=blue]
              > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
              >> var timer = setTimeout(docu ment.forms[theForm.name].submit, 2000);[/color]
              >
              > Does this work consistently?[/color]

              I'm pretty sure it does.
              [color=blue]
              > I can imagine implementations where the submit function is the same for
              > all forms, and relies on the "this" reference to find out which form to
              > submit. That would break here because the function is called with the
              > global object as "this".
              >
              > I would write
              > setTimeout(func tion(){document .forms[theForm.name].submit();},200 0); to
              > be safe, but I don't know if there is a browser where the former version
              > would break.[/color]

              Sorry, I fail to see the difference between the `this' reference in a
              submit() method called from a global wrapper function in the global
              execution context, and the `this' reference in a submit() method called
              directly in the global execution context.


              PointedEars

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: setTimeout not executing form submit

                Thomas 'PointedEars' Lahn wrote:
                [color=blue]
                > Lasse Reichstein Nielsen wrote:[color=green]
                >> Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=darkred]
                >>> var timer = setTimeout(docu ment.forms[theForm.name].submit, 2000);[/color]
                >>
                >> Does this work consistently?
                >> I can imagine implementations where the submit function is the same for
                >> all forms, and relies on the "this" reference to find out which form to
                >> submit. That would break here because the function is called with the
                >> global object as "this".
                >>
                >> I would write
                >> setTimeout(func tion(){document .forms[theForm.name].submit();},200 0); to
                >> be safe, but I don't know if there is a browser where the former version
                >> would break.[/color]
                >
                > Sorry, I fail to see the difference between the `this' reference in a
                > submit() method called from a global wrapper function in the global
                > execution context, and the `this' reference in a submit() method called
                > directly in the global execution context.[/color]

                To clarify this: AIUI the above is semantically equivalent to

                // The real constructor would be an internal implementation
                // of W3C DOM's HTMLFormElement interface
                function HTMLFormElement ()
                {
                }

                // By making submit() a prototype method (as it actually is),
                // I have a "submit function [that] is the same for all forms
                // and relies on the 'this' reference to find out which form
                // to submit." as Lasse described.
                HTMLFormElement .prototype.subm it = function submit()
                {
                // The form is submitted here. I also return
                // the `this' reference for test purposes.
                return this;
                }

                // When the document is parsed, "form" elements
                // are created as HTMLFormElement DOM objects
                document.forms[theForm.name] = new HTMLFormElement ;

                // Retrieve a reference to the submit() method of that new object
                // as the script engine does internally (see ECMAScript 3)
                var theFormSubmit = document.forms[theForm.name].submit;

                // I use the reference as argument for setTimeout
                var timer = setTimeout(theF ormSubmit, 2000);

                setTimeout() is a method of the global object, so the object calling
                theFormSubmit() is always the global object. The `this' reference
                nevertheless refers to the respective HTMLFormElement object, no matter
                if theFormSubmit() is called like the above (my solution) or in a wrapper
                function (as Lasse's solution):

                // The wrapper function that Lasse defined as anonymous;
                // I use a named function to show the reference.
                function lrnSubmitWrappe r()
                {
                // I return the retrieved `this' reference for test purposes
                return theFormSubmit() ;
                }

                // I use the reference to the wrapper function as argument;
                // again: that's what the script engine does internally.
                var timer = setTimeout(lrnS ubmitWrapper, 2000);

                That's quite easy to test:

                alert(theFormSu bmit() == lrnSubmitWrappe r()); // true
                alert(theFormSu bmit() === lrnSubmitWrappe r()); // true

                AFAIS the only thing that differs here is the stack (trace).
                Am I missing something?


                PointedEars

                Comment

                • Michael Winter

                  #9
                  Re: setTimeout not executing form submit

                  On Thu, 22 Apr 2004 18:00:06 +0200, Thomas 'PointedEars' Lahn
                  <PointedEars@we b.de> wrote:

                  [snip]
                  [color=blue]
                  > Sorry, I fail to see the difference between the `this' reference in a
                  > submit() method called from a global wrapper function in the global
                  > execution context, and the `this' reference in a submit() method called
                  > directly in the global execution context.[/color]

                  It's a big one, as this snippet should show:

                  var obj = new ( function() {
                  var base = this;

                  this.method = function() {
                  alert( this == base );
                  };
                  });
                  var method = obj.method;

                  obj.method();
                  method();

                  In the first call, using a qualified reference, the this operator
                  correctly refers to the object, obj. In the second, unqualified call, the
                  this operator is not set as desired. It will, in fact, refer to the global
                  object. You would need to use call() to correctly apply the second
                  approach.

                  I learnt this in a similar way, using setTimeout() to directly call a
                  method.

                  Mike

                  --
                  Michael Winter
                  M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: setTimeout not executing form submit

                    Michael Winter wrote:
                    [color=blue]
                    > On Thu, 22 Apr 2004 18:00:06 +0200, Thomas 'PointedEars' Lahn
                    > <PointedEars@we b.de> wrote:[/color]

                    Please shorten your attribution.
                    [color=blue]
                    > [snip]
                    >[color=green]
                    >> Sorry, I fail to see the difference between the `this' reference in a
                    >> submit() method called from a global wrapper function in the global
                    >> execution context, and the `this' reference in a submit() method called
                    >> directly in the global execution context.[/color]
                    >
                    > It's a big one, as this snippet should show:
                    >
                    > var obj = new ( function() {
                    > var base = this;
                    >
                    > this.method = function() {
                    > alert( this == base );
                    > };
                    > });
                    > var method = obj.method;
                    >
                    > obj.method();
                    > method();[/color]

                    I already know that. The discussion you snipped was about using a
                    wrapper function or not. Taking your example, my tests show that it
                    makes no difference to `this' if obj.method() is called directly or
                    via

                    function foo()
                    {
                    obj.method();
                    }

                    foo();

                    since the calling object is always `obj'.

                    BTW: Your From address violates Internet/NetNews standards.


                    PointedEars

                    Comment

                    • Richard Cornford

                      #11
                      Re: setTimeout not executing form submit

                      Thomas 'PointedEars' Lahn wrote:[color=blue]
                      > Thomas 'PointedEars' Lahn wrote:[/color]
                      <snip>[color=blue][color=green]
                      >> Sorry, I fail to see the difference between the `this' reference in a
                      >> submit() method called from a global wrapper function in the global
                      >> execution context, and the `this' reference in a submit() method
                      >> called directly in the global execution context.[/color]
                      >
                      > To clarify this: AIUI the above is semantically equivalent to
                      >
                      > // The real constructor would be an internal implementation
                      > // of W3C DOM's HTMLFormElement interface
                      > function HTMLFormElement ()
                      > {
                      > }
                      >
                      > // By making submit() a prototype method (as it actually is),
                      > // I have a "submit function [that] is the same for all forms
                      > // and relies on the 'this' reference to find out which form
                      > // to submit." as Lasse described.
                      > HTMLFormElement .prototype.subm it = function submit()
                      > {
                      > // The form is submitted here. I also return
                      > // the `this' reference for test purposes.
                      > return this;
                      > }
                      >
                      > // When the document is parsed, "form" elements
                      > // are created as HTMLFormElement DOM objects
                      > document.forms[theForm.name] = new HTMLFormElement ;
                      >
                      > // Retrieve a reference to the submit() method of that new object
                      > // as the script engine does internally (see ECMAScript 3)
                      > var theFormSubmit = document.forms[theForm.name].submit;
                      >
                      > // I use the reference as argument for setTimeout
                      > var timer = setTimeout(theF ormSubmit, 2000);
                      >
                      > setTimeout() is a method of the global object, so the object calling
                      > theFormSubmit() is always the global object. The `this' reference
                      > nevertheless refers to the respective HTMLFormElement object, no
                      > matter if theFormSubmit() is called like the above (my solution) or
                      > in a wrapper function (as Lasse's solution):
                      >
                      > // The wrapper function that Lasse defined as anonymous;
                      > // I use a named function to show the reference.
                      > function lrnSubmitWrappe r()
                      > {
                      > // I return the retrieved `this' reference for test purposes
                      > return theFormSubmit() ;
                      > }
                      >
                      > // I use the reference to the wrapper function as argument;
                      > // again: that's what the script engine does internally.
                      > var timer = setTimeout(lrnS ubmitWrapper, 2000);
                      >
                      > That's quite easy to test:
                      >
                      > alert(theFormSu bmit() == lrnSubmitWrappe r()); // true
                      > alert(theFormSu bmit() === lrnSubmitWrappe r()); // true
                      >
                      > AFAIS the only thing that differs here is the stack (trace).
                      > Am I missing something?[/color]

                      Yes, your test is being skewed by assigning a reference to the function
                      object, that has been assigned to the prototype of HTMLFormElement , to a
                      global variable. The effect is that the function is called in the same
                      way in each context.

                      In Lasse's example the wrapper function took the form:-

                      function(){
                      document.forms[theForm.name].submit();
                      }

                      - Making it a - CallExpression -> MemberExpressio n Arguments - (with an
                      empty arguments list). MemberExpressio n evaluates to an internal
                      Reference type with - document.forms[theForm.name] - as its base object
                      and "submit" as its property name, when executed -
                      document.forms[theForm.name] - becomes the - this - object.

                      But in:-

                      function(){
                      return theFormSubmit() ;
                      }

                      The identifier - theFormSubmit - evaluates as a Reference type with the
                      global object as the base object and the property name "theFormSubmit" .
                      So the - this - object for the execution of the function is the global
                      object (at least that is what the specs say should happen).

                      Unsurprisingly:-

                      | alert(theFormSu bmit() == lrnSubmitWrappe r()); // true
                      | alert(theFormSu bmit() === lrnSubmitWrappe r()); // true

                      - alerts true as the function is being executed with the - this -
                      reference set to the global object in both cases.

                      Personally I would expect the original:-

                      | setTimeout(docu ment.forms[theForm.name].submit, 2000);

                      - to be problematic for exactly the reasons that Lasse did. Although the
                      MemberExpressio n - document.forms[theForm.name].submit - in the
                      arguments list for setTimeout will evaluate as a Reference type that
                      has - document.forms[theForm.name] - as its base object and the property
                      name "submit", the evaluation of the arguments list will call the
                      internal GetValue function, passing that Reference as an argument and
                      returning a value that is a function object. Any association with the -
                      document.forms[theForm.name] - object is lost at this point.

                      In principle, when setTimeout calls that function object it will be as a
                      property of the Activation object belonging to its execution context.
                      And step 7 in the Function Calls algorithm (section 11.2.3) requires
                      function properties of an Activation object to be called with a null -
                      this - value (which means the global object will be used in the pace of
                      null during the function call).

                      Richard.


                      Comment

                      • Yann-Erwan Perio

                        #12
                        Re: setTimeout not executing form submit

                        Thomas 'PointedEars' Lahn wrote:
                        [color=blue][color=green]
                        >>Sorry, I fail to see the difference between the `this' reference in a
                        >>submit() method called from a global wrapper function in the global
                        >>execution context, and the `this' reference in a submit() method called
                        >>directly in the global execution context.[/color][/color]

                        Some browsers, like Mozilla, implement host objects in a prototype-like
                        fashion; in this case, it is not unrealistic to consider there's a sort
                        of "this" value which is internally handled, and that the determination
                        of this "this" value follows the same rules as in javascript. Hence
                        Lasse's answer, which is exactly what should be done IMHO.
                        [color=blue]
                        > // Retrieve a reference to the submit() method of that new object
                        > // as the script engine does internally (see ECMAScript 3)
                        > var theFormSubmit = document.forms[theForm.name].submit;[/color]

                        That's incorrect, you'd be getting the submit *function*, but the "this"
                        value would be the global object(see ECMAScript 3:-)) - if the function
                        knows the "this" value, then that's an IE-like behavior, not a
                        javascript one.

                        var x="48";
                        var foo={bar:functi on(){alert(this .x)}, x:"42"};
                        var foobar=foo.bar;
                        foo.bar(); //42
                        foobar(); //48

                        [color=blue]
                        > setTimeout() is a method of the global object, so the object calling
                        > theFormSubmit() is always the global object.[/color]

                        That's probably true, still you simplify too much; as an argument of the
                        setTimeout function, the way theFormSubmit is called (that is, how its
                        scope chain is defined) depends on setTimeout implementation, not of
                        setTimeout "base object".

                        Still, what would you expect from:

                        var foo=1;
                        setTimeout("ale rt(this.foo)", 1);
                        setTimeout.appl y({foo:2}, ["alert(this.foo )", 1]);
                        [color=blue]
                        > function lrnSubmitWrappe r()
                        > {
                        > // I return the retrieved `this' reference for test purposes
                        > return theFormSubmit() ;[/color]

                        Nope, Lasse has fully qualified the submit method... Did you try your
                        example in Mozilla, or only IE;-)


                        HTH
                        Yep.

                        Comment

                        • Michael Winter

                          #13
                          Re: setTimeout not executing form submit

                          On Thu, 22 Apr 2004 19:51:14 +0200, Thomas 'PointedEars' Lahn
                          <PointedEars@we b.de> wrote:
                          [color=blue]
                          > Michael Winter wrote:
                          >[color=green]
                          >> On Thu, 22 Apr 2004 18:00:06 +0200, Thomas 'PointedEars' Lahn
                          >> <PointedEars@we b.de> wrote:[/color]
                          >
                          > Please shorten your attribution.[/color]

                          You know I'm not going to, so there isn't much point in asking. :P

                          [snip]
                          [color=blue]
                          > I already know that.[/color]

                          Probably, but if so, you're missing the point.
                          [color=blue]
                          > The discussion you snipped was about using a wrapper function or not.[/color]

                          I knew precisely what your discussion was about, and my post addressed it.
                          [color=blue]
                          > Taking your example, my tests show that it makes no difference to
                          > `this' if obj.method() is called directly or via
                          >
                          > function foo()
                          > {
                          > obj.method();
                          > }
                          >
                          > foo();
                          >
                          > since the calling object is always `obj'.[/color]

                          I know, but that isn't the case when you omit the calling object, which is
                          effectively what happens when you pass setTimeout() a function reference.
                          This leads to it being called unqualified (that is, with "this" set to the
                          global object), which might lead to an error. Wrapping a qualified
                          reference in a globally defined function (or an anonymous one in this
                          case), avoids that. Replace

                          method();

                          with

                          setTimeout( obj.method, 1000 );

                          and you'll see that they're equivalent. Then perform

                          setTimeout( function() { obj.method(); }, 1000 );

                          and you'll see that it and

                          obj.method();

                          are equivalent.
                          [color=blue]
                          > BTW: Your From address violates Internet/NetNews standards.[/color]

                          I know. You've already told me. However, people still e-mail me, so it
                          can't that big a deal.

                          Mike

                          --
                          Michael Winter
                          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                          Comment

                          • Lasse Reichstein Nielsen

                            #14
                            Re: setTimeout not executing form submit

                            Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                            [color=blue]
                            > // The wrapper function that Lasse defined as anonymous;
                            > // I use a named function to show the reference.
                            > function lrnSubmitWrappe r()
                            > {
                            > // I return the retrieved `this' reference for test purposes
                            > return theFormSubmit() ;
                            > }[/color]

                            That was not what I wrote. Rather, I called the function as a method
                            of an object.
                            [color=blue]
                            > // I use the reference to the wrapper function as argument;
                            > // again: that's what the script engine does internally.
                            > var timer = setTimeout(lrnS ubmitWrapper, 2000);
                            >
                            > That's quite easy to test:
                            >
                            > alert(theFormSu bmit() == lrnSubmitWrappe r()); // true
                            > alert(theFormSu bmit() === lrnSubmitWrappe r()); // true[/color]

                            Yes, but also:
                            alert(theFormSu bmit() == window); // true
                            [color=blue]
                            > AFAIS the only thing that differs here is the stack (trace).
                            > Am I missing something?[/color]

                            Yes, when a function is being called as a method and when it is being
                            called as a pure function.

                            Take this as a simple example:

                            <script type="text/javascript">
                            var foo = 42; // global variable
                            var obj = { foo : 37,
                            method : function() { alert(this.foo) ; } };

                            setTimeout(obj. method,1000);
                            setTimeout(func tion(){obj.meth od();},2000);
                            </script>

                            If the two approaches are equivalent, then the alerts should be the same.
                            They are not. The first is 42, showing that the method has lost its
                            association to "obj", while the second is 37.

                            The same can happen for form submits. Passing the form submit function
                            alone can leave a function that doesn't know which form to submit, while
                            passing the wrapped method call will always work.

                            /L
                            --
                            Lasse Reichstein Nielsen - lrn@hotpop.com
                            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                            'Faith without judgement merely degrades the spirit divine.'

                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              Re: setTimeout not executing form submit

                              Lasse Reichstein Nielsen wrote:
                              [color=blue]
                              > <script type="text/javascript">
                              > var foo = 42; // global variable
                              > var obj = { foo : 37,
                              > method : function() { alert(this.foo) ; } };
                              >
                              > setTimeout(obj. method,1000);
                              > setTimeout(func tion(){obj.meth od();},2000);
                              > </script>
                              >
                              > If the two approaches are equivalent, then the alerts should be the same.
                              > They are not. The first is 42, showing that the method has lost its
                              > association to "obj", while the second is 37.[/color]

                              I see. The first one is merely a reference to some Function
                              object from the view of the calling global/window object!
                              Thank you very much for your explanation.
                              [color=blue]
                              > The same can happen for form submits. Passing the form submit function
                              > alone can leave a function that doesn't know which form to submit, while
                              > passing the wrapped method call will always work.[/color]

                              ACK, though I would prefer

                              setTimeout(func tion foo() { obj.method(); }, 2000);

                              for backwards compatibility.


                              PointedEars

                              Comment

                              Working...