Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:13:27+00:00 2026-05-29T06:13:27+00:00

All, See the code below: function menu() { this.menuitem=[]; this.submenu=[]; this.menuitem[0] = $(‘div#sivname1’); this.menuitem[1]

  • 0

All,

See the code below:

function menu() {
    this.menuitem=[];
    this.submenu=[];
    this.menuitem[0] = $('div#sivname1');
    this.menuitem[1] = $('div#divname2');
    this.submenu[0] = $('div#submenu1');
    this.submenu[1] = $('div#submenu2');
    this.active = false;
    this.timeout;
}

menu.prototype = {
    animatedown: function(submenu) {
        submenu.animate({top: '99px'}, 200);
    },
    animateup: function(submenu) {
        submenu.animate({top: '-4px'}, 200);
    }
}

var menu = new menu();
z=2;
while(z--) {
    console.log(z);
    menu.menuitem[z].hover(
    function() { //mouseover
        if(menu.active) {
            clearTimeout(menu.submenu[z].data("timeout"));
        }
        else {
            menu.animatedown(menu.submenu[z])
        };
    },
    function() { //mouseleave
        $(this).data("timeout", setTimeout(function({
            menu.animateup(menu.submenu[z])
        },200));
        menu.active = false;    
    }),

    menu.submenu[z].hover(
    function() { //mouseover
        menu.active = true;
        if (menu.menuitem[z].data("timeout")) {
            clearTimeout(menu.menuitem[z].data("timeout"));
        };
    }, 
    function() { //mouseleave
        $(this).data("timeout", setTimeout(function() {
            menu.animateup(menu.submenu[z]);menu.active = false;
        },200));   
    });
}

This code gives the following error:

Uncaught TypeError: Cannot call method ‘animate’ of undefined

And the strange this is that is I add:

z=0; 

to the bottom of the code it will work properly. I would expect it to work without z=0 and I have no idea why it does when I add that. Can anyone please explain?

The problem was in the closures, the following code works:

z=1;
while(z--){
  (function(z) {
    console.log(z);
      menu.menuitem[z].hover(
        function(){ //mouseover
          if(menu.active){clearTimeout(menu.submenu[z].data("timeout"));}
          else{menu.animatedown(menu.submenu[z])};
        }, 
        function(){ //mouseleave
          $(this).data("timeout", setTimeout(function(){menu.animateup(menu.submenu[z])},200));
          menu.active = false;    
        }
      )

      menu.submenu[z].hover(
        function(){ //mouseover
          menu.active = true;
          if(menu.menuitem[z].data("timeout")){clearTimeout(menu.menuitem[z].data("timeout"));};
        }, 
        function(){ //mouseleave
          $(this).data("timeout", setTimeout(function(){menu.animateup(menu.submenu[z]);menu.active = false;},200));   
        }
      );
  })(z); //this is to enable closures http://bonsaiden.github.com/JavaScript-Garden/#function.closures
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:13:28+00:00Added an answer on May 29, 2026 at 6:13 am

    First of all, you have an error in the code:

    $(this).data("timeout", setTimeout(function({
        menu.animateup(menu.submenu[z])
    },200));
    

    Should be:

    $(this).data("timeout", setTimeout(function() {
        menu.animateup(menu.submenu[z]);
    },200);
    

    Second: z is global, and your callbacks will use it’s last value after the initialization. You should create a local copy of z for your callbacks with a closure in the loop, like this:

    while (z--) {
        (function(z) {
            /* your code */
        })(z);
    }
    

    This way you enclose the value of z for that while cycle, and the callbacks in it.

    The strange thing with z=0 is that, when the loop dies (since z is 0) the decrementing operator will run one more time, and z will be -1. The -1 index will be used in all your callbacks, indexing a non-object which don’t have animate ofc. But (!) if you set it z=0, than it will index an object, but always the one with the 0 index. You have to use the above correction to get the good result in every menuitem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See the code example below: public function buildForm(FormBuilder $builder, array $options) { $builder->add('firstname'); $builder->add('lastname');
According to Herb Sutter the code below wouldn't compile. See this site http://www.gotw.ca/gotw/066.htm from
Delphi XE. Windows 7. There is a function (please see a code below) or
Noob question here. Please see code below. Where can I read more on this?
UPDATE The issue is solved, all the code you can see works. Hello! I
I see all this heavy Flash/3D websites and it's making me wonder how do
I have added a show more or less function to a div - this
Please see the jQuery code below, it used to paginate some search results paginate:
I see examples where JavaScript code including jQuery and jslint use the notation below:
See the code below - I am trying to put a const object into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.