Results 1 to 4 of 4

Thread: Using a function as a parameter of another one

  1. #1
    Join Date
    Dec 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Using a function as a parameter of another one

    hi everyone,
    I think my question should be simple, but I'm just not too falmiliar with all syntax tricks. I'd like to call a function hide(id) in the setTimeout() function, but hide has it's own params - (id) - and once inside setTimeout, id becomes undefinied.. Here's the example, to make myself clear:


    function show(id, file) {
    document.getElementById(id).src = file;
    setTimeout("hide(id)", 1000);
    }


    there should be a way to put a parametered function inside setTimeout, no? cuz actually all examples I could find were like setTimeout("hide()", 1000), hide being empty...
    oh, and here's the hide() function just in case:

    function hide(id){
    document.getElementById(id).src = 'back.gif';
    }


    thanks for your help!!

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    You can certainly pass a parameter into a function being called by setTimeout(). In this case:

    Code:
    function show(id, file) {
    document.getElementById(id).src = file;
    setTimeout("hide('test')", 1000);
    }
    This assumes that "test" is the ID of the element you wish to "hide."

  3. #3
    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

    Yes but, it gets tricky when using a parameter of the function that is setting the timeout as a parameter of the function set by the timeout. Think about it.

    The problem is that the original function's parameter is a local variable or something like one. However, by the time the timeout fires, it is looking for its value in the global scope. This can be overcome by passing the original parameter as a local variable written to the timeout call as a literal:

    Code:
    function show(id, file) {
    document.getElementById(id).src = file;
    setTimeout("hide("+id+")", 1000);
    }
    or sometimes:

    Code:
    function show(id, file) {
    document.getElementById(id).src = file;
    setTimeout("hide('"+id+"')", 1000);
    }
    can be required.
    - John
    ________________________

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

  4. #4
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    John is right, ignore my solution ellyz, I didn't notice that "id" is passed in as a parameter of show(). In that case John's second example would work:

    Code:
    <img id="test" src="photo1.jpg" />
    
    <script type="text/javascript">
    
    function show(id, file) {
    document.getElementById(id).src = file;
    setTimeout("hide('"+id+"')", 1000);
    }
    
    function hide(id){
    document.getElementById(id).src = 'photo3.jpg';
    }
    
    show("test", "photo2.jpg")
    
    </script>
    Thanks for catching that John.

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
  •