Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts

    settimeout scope ?

    mainly interested in how to make "/*1.*/" to work. Thanks for help.


    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html class = "html">
      <head class = "head">
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Untitled Document</title>
        <script language="javascript" type="text/javascript">
        function MyObj()
        {
          this.doIt =  function()
          {
            /*0.*/ test2('b');                                           //works
            /*1.*/ var t=setTimeout("test2('a')",1000);        //does not
            /*2.*/ var t=setTimeout("this.test('a')",1000);    //does not
            /*3.*/ var t=setTimeout("myObj.test('a')",1000); //works  
          };
          
          this.test =  function(param)
          {
            alert(param);
          };
          
          function test2(param)
          {
           alert(param);
          }
        }
         
        </script>  
        <!--end of need this package for css event selectors--> 
      </head>
      <body style = 'height:100%' onload="myObj = new MyObj();" >
      <button value="fdjgsda" onclick="myObj.doIt();"> </button>
      </body>
    </html>

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    setTimeout, if given a string argument, compiles that string into the body of a function in the global scope. However, using a string argument is discouraged, so don't do that.

    setTimeout can take a function object as it's first argument instead of a string. This function will be called from the global scope, but because JavaScript has closures, the body of the function will keep the local scope in which it was declared. Thus
    Code:
    /* 1. */var t=setTimeout(function(){test2('a')},1000);
    /* 2. alt 1. */var me=this,t=setTimeout(function(){me.test('a')},1000);
    /* 2. alt 2. */var t=setTimeout((function(me){return function(){me.test('a')}})(this),1000);
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Thanks. Solved my problem.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •