Results 1 to 5 of 5

Thread: problem passing parameters to function that is a parameter itself in setTimeout()

  1. #1

    Thread Starter
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292

    problem passing parameters to function that is a parameter itself in setTimeout()

    Hello

    I am having a small problem which I thought I resolved a while back...unfortunately it's back

    it's a dropdown layer that waits 1 second before closing.


    here is my code



    Code:
    <script>
    var delay;
    function swap()
    { 
       var nameOfLayer = swap.arguments[0];
       if(window.delay)
     {
      clearTimeout(delay);
     }
      
       if(swap.arguments[1] == 1)
     {
        document.getElementById(nameOfLayer).style.visibility = "visible";
     }
     else
     {
      document.getElementById(nameOfLayer).style.visibility = "hidden";
     }
    }
    
    function wait()
    {
     var n = wait.arguments[0];
     delay = setTimeout("swap(' + n + ',2)",1000);
    
               //   if i change ' + n + ' to the name of layer it works fine
               //   delay = setTimeout("swap('lay1',2)",1000);
    
    
    }
    
    </script>

    here is how I call the function


    Code:
     <p><a href = "" onMouseover="swap('lay1',1)" onMouseout="wait('lay1')" >Wawryn Classroom</a></p>

    i always have problems passing parameters to a function that is itself a parameter of the setTimeout function

    any help will be appreciated

    bsw

  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    You've almost got it. In your wait function change the ' + n + ' to '" + n + "'.

    Basically the string is not being compiled correctly. Here's what the function would look like.

    Code:
    function wait()
    {
     var n = wait.arguments[0];
     delay = setTimeout("swap('" + n + "',2)",1000);
    }
    I'd also recommend placing your arguments as named parameters of the function. It is easier to read and can save some coding.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  3. #3

    Thread Starter
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292
    right on

    this will work

    ya i normaly put the params in, but i was being lazy

    thanks for the tips

    bsw

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You chose a more difficult, longer to write and more complicated approach because you were LAZY?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292
    yes

    if I wouldn't be lazy I would read more javascript tutorials and wouldn't be getting stuck every two seconds


    what is the correct approach CornedBee?

    how can I make it more efficient and generic

    bsw

Posting Permissions

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



Click Here to Expand Forum to Full Width