Results 1 to 7 of 7

Thread: setTimeout() help

  1. #1
    Join Date
    Nov 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default setTimeout() help

    I am trying to create a onmouseover that is active after a certain amount of time. It does not seem to work.

    My code below:

    HTML Code:
    <html>
    <head>
    
    <script type="text/javascript">
    
    function hiddentext(item)
    {
    var t=setTimeout(open(item),10000)
    
    }
    function open(banner){
    	document.getElementById(banner).style.display='block';
    }
    
    function clear(){
    	cearTimeout(t);
    }
    </script>
    
    
    </head>
    <body>
    
    <div id="panel" style="background:#cccccc;display:none;">
    Some information
    </div>
    
    <a href="#" onmouseover="hiddentext('panel');" onmouseout="clear();">Open</a>
    
    
    </body>
    </html>
    Any help appreciated

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Code:
    var t=setTimeout('open('+item+')',10000)
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Nov 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    HI Thanks for reply.

    I thought you only need + for concatenate.

    Im not doing that just sticking the variable straight in. It doesnt work anyway.

    Its just the delay that is not working?

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    10 seconds is very long, anyway:
    Code:
    function hiddentext(item)
    {
    open(item);
    
    }
    function open(banner){
    	var t=setTimeout(function()
    		{
    			document.getElementById(banner).style.display='block';
    		},1000); // Change second param if you intend to work on 10sec.
    }
    function clear(){
    	cearTimeout(t);
    }
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. #5
    Join Date
    Nov 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    HI mate,

    THanks works a treat.

    But i dont understand why you use this "function()"?

    Are you creating a new function inside the event?

    Thanks,

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The setTimeout() method executes whatever it is set to execute in the global scope. Unless it has a parameter(s) fed to it at the time it is created, it will only have the global scope to look to for their values. Using string concatenation (will not work with objects) is one method for limiting the scope to a value that exists in the scope where setTimeout is created. Creating a new function (preferred - works with all types of values) in a scope where the parameter is defined and cannot change is another.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    On your original mistake, http://fn-js.info/snippets/bind has some info, as well as a helper function to make the whole thing a little simpler.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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