Development javascript, setTimeout scope

Discussion in 'Software' started by Ben, 8 Dec 2006.

  1. Ben

    Ben What's a Dremel?

    Joined:
    11 Aug 2003
    Posts:
    1,000
    Likes Received:
    0
    Well 'setTimeout' seems to have a very odd object scope, and i just cant get my head round what to do to fix it.

    I think its best to have a look at the code below as i know very little javascript and don't think it will make much sense trying to explain it.

    The error comes up as 'GReset is not defined'.

    If i move GReset outside of Gshell, its then in the scope of setTimeout, but that does not help as i need it to be part of the Gshell object (is it even an object?).
    If i replace setTimeout('GReset()','1000'); with GReset(); it works but there is no delay.

    Now as far as i can see, i need to somehow pass it a reference to the object and the GReset function.

    Well here is a cut down version of what I'm having problems with:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
        <title>Test Page</title>
    </head>
    <body>
        <div id="IVshell">
            <span class="selected"><a href="#" rel="pagenav[0]">Blog</a></span>
            <span class="base"><a href="#" rel="pagenav[1]">Photography</a></span>
            <span class="base"><a href="#" rel="pagenav[2]">Work</a></span>
            <span class="base"><a href="#" rel="pagenav[3]">Projects</a></span>
            <span class="base"><a href="#" rel="pagenav[4]">Contact</a></span>
        </div>
    <script type="text/javascript">
    function Gshell ()
    {
            var BaseNode = document.getElementById('IVshell');
            if(!BaseNode) return;
    
            var BaseLinks = BaseNode.getElementsByTagName('a');
            if(!BaseLinks) return;
            
            var wait = 0;
    
            function GReset ()
            {
                wait--;
                if (wait > '0') return;
                
                alert('hi');
            }
            
            
            function GHandle ()
            {
                for(var i = 0; i < BaseLinks.length; i++)
                {
                BaseLinks[0].onmouseout = function() {wait++; setTimeout('GReset()','1000');}
                }            
            }    
            
            GHandle();
    }
            
    Gshell();
    </script>
    </body>
    </html>
    
    
    Ben
     
  2. [Jonny]

    [Jonny] What's a Dremel?

    Joined:
    1 Sep 2003
    Posts:
    296
    Likes Received:
    0
    Doesn't look like it's a scope issue, try this: (anonymous functions pwn :p)

    Code:
    window.setTimeout(function() { GReset(); }, 1000)
     
  3. Ben

    Ben What's a Dremel?

    Joined:
    11 Aug 2003
    Posts:
    1,000
    Likes Received:
    0
    oh my, i would have never got that :) . I don't like how messy it looks thou :blah: .

    Thanks,
    Ben.
     

Share This Page