settimeout and this keyword

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Risser

    settimeout and this keyword

    Hi everyone,

    I am modifying the suckerfish dropdown code to use settimeout to have
    a slight pause before the menus disappear to make it more user
    friendly. I have hit a snag with the following statement:

    li.onmouseout=f unction()
    {
    timerID=setTime out('this.getEl ementsByTagName ("UL")
    [0].style.display = "none"', timecount);
    }

    If I put take the statement

    this.getElement sByTagName("UL" )[0].style.display = "none";

    out of the settimeout function, it works. (w/o the pause, obviously).
    With the settimeout function, I get "this.getElemen tByTagName is not a
    function" error.

    I have tried putting this.getElement sByTagName("UL" )[0] is a variable
    and then using that in the settimeout function, but then each li will
    only open the very last menu in the list. (ie all the menu items open
    the last sub menu)

    Here is a link to a stripped down version of the code.


    Thanks everyone!

  • scripts.contact

    #2
    Re: settimeout and this keyword

    On Apr 3, 2:20 pm, "Adam Risser" <aris...@gmail. comwrote:
    Hi everyone,
    >
    I am modifying the suckerfish dropdown code to use settimeout to have
    a slight pause before the menus disappear to make it more user
    friendly. I have hit a snag with the following statement:
    >
    li.onmouseout=f unction()
    {
    timerID=setTime out('this.getEl ementsByTagName ("UL")
    [0].style.display = "none"', timecount);
    timerID=setTime out(function(){
    this.getElement sByTagName("UL" )[0].style.display = "none"
    }, timecount);
    >
    }

    Comment

    • Walton

      #3
      Re: settimeout and this keyword

      On Apr 3, 3:57 pm, "scripts.contac t" <scripts.cont.. .@gmail.com>
      wrote:
      On Apr 3, 2:20 pm, "Adam Risser" <aris...@gmail. comwrote:
      >
      Hi everyone,
      >
      I am modifying the suckerfish dropdown code to use settimeout to have
      a slight pause before the menus disappear to make it more user
      friendly. I have hit a snag with the following statement:
      >
      li.onmouseout=f unction()
      {
      timerID=setTime out('this.getEl ementsByTagName ("UL")
      [0].style.display = "none"', timecount);
      >
      this solution doesn't work:
      timerID=setTime out(function(){
      you can't use the "this" keyword here. you are no longer in the scope
      of the "li" element, hence it "this" no longer points to the li
      element.
      this.getElement sByTagName("UL" )[0].style.display = "none"
      }, timecount);
      >
      >
      >
      }

      try this code instead:


      li.onmouseout=f unction() {
      var that = this; //create a pointer to this reference
      timerID=setTime out(function(){
      that.getElement sByTagName("UL" )[0].style.display = "none"
      }, 500);
      }

      Comment

      • Adam Risser

        #4
        Re: settimeout and this keyword

        On Apr 3, 5:16 pm, "Walton" <jrhol...@gmail .comwrote:
        On Apr 3, 3:57 pm, "scripts.contac t" <scripts.cont.. .@gmail.com>
        wrote:
        >
        On Apr 3, 2:20 pm, "Adam Risser" <aris...@gmail. comwrote:
        >
        Hi everyone,
        >
        I am modifying the suckerfish dropdown code to use settimeout to have
        a slight pause before the menus disappear to make it more user
        friendly. I have hit a snag with the following statement:
        >
        li.onmouseout=f unction()
        {
        timerID=setTime out('this.getEl ementsByTagName ("UL")
        [0].style.display = "none"', timecount);
        >
        this solution doesn't work:
        >
        timerID=setTime out(function(){
        >
        you can't use the "this" keyword here. you are no longer in the scope
        of the "li" element, hence it "this" no longer points to the li
        element.
        >
        this.getElement sByTagName("UL" )[0].style.display = "none"
        }, timecount);
        >
        }
        >
        try this code instead:
        >
        li.onmouseout=f unction() {
        var that = this; //create a pointer to this reference
        timerID=setTime out(function(){
        that.getElement sByTagName("UL" )[0].style.display = "none"
        }, 500);
        >
        }
        Thank you, that was part of the problem! it also took adding an extra
        statement to close any active menu.

        I am going to google around for some scope tutorials. Thanks again!

        Comment

        • RobG

          #5
          Re: settimeout and this keyword

          On Apr 4, 10:42 pm, "Adam Risser" <aris...@gmail. comwrote:
          [...]
          Thank you, that was part of the problem! it also took adding an extra
          statement to close any active menu.
          As a usability issue, consider cancelling the timeout if the cursor
          goes back over the menu before it is closed.
          >
          I am going to google around for some scope tutorials. Thanks again!
          Try the following, since you are using a closure to fix the issue:

          <URL: http://www.jibbering.com/faq/faq_notes/closures.html >


          --
          Rob

          Comment

          Working...