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

  • SEARCH
  • Home
  • 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 8707499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:53:04+00:00 2026-06-13T03:53:04+00:00

I have always had trouble working with time events. Could someone please explain why

  • 0

I have always had trouble working with time events. Could someone please explain why A doesn’t work and B does? The only difference is in A I put the event binding in a function. Don’t worry about the function close, it has nothing to do with the question. When I test A, there is no js errors but timer is not cleared.

A ->

Test.Navigation = (function() {

    var openTimer = null;
    var closeTimer = null;

    var addListeners = function() {
        $('.hover_container').on('mousemove', function(e) {

            clearTimeout(closeTimer);

        });

        $('.hover_container').on('mouseleave', function(e) {

            // set the close timer
            var container = this; 
            closeTimer = setTimeout(function() {
                //has the mouse paused
                close(container);
            }, 750);
        });

    };

    return {
        init : function() {
            addListeners();
        }
    };

})();

B ->

Test.Navigation = (function() {

    var openTimer = null;
    var closeTimer = null;

    $('.hover_container').on('mousemove', function(e) {

        clearTimeout(closeTimer);

    });

    $('.hover_container').on('mouseleave', function(e) {

        // set the close timer
        var container = this; 
        closeTimer = setTimeout(function() {
            //has the mouse paused
            close(container);
        }, 750);
    });


    var addListeners = function() {
        // nothing here
    };

    return {
        init : function() {
            addListeners();
        }
    };

})();

Edit: Please ignore the container part, it has nothing to dow ith the question it is simply part of the full code that I did not take out

  • 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-06-13T03:53:05+00:00Added an answer on June 13, 2026 at 3:53 am

    A is binded before the object exists where the init is called. Because your return a new object. If you are using, 2 objects are created. 1 with the vars en binds. and 1 with the returns.

    B is working because you create a function where the elements are initialized and use the right scope. A is not working because the bindings are on the wrong scope because your create 2 objects:

    new Test.Navigation(); // Create 1 object
    
    // Create second object.
    return {
        init : function() {
            addListeners();
        }
    };
    

    Youd better get a structure like this, then it should work aswell:

    Test.Navigation = (function() {
        // Private vars. Use underscore to make it easy for yourself so they are private.
        var _openTimer = null,
            _closeTimer = null;
    
        $('.hover_container').on('mousemove', function(e) {
            clearTimeout(_closeTimer );
        });
    
        $('.hover_container').on('mouseleave', function(e) {
    
            // set the close timer, 
            //    use $.proxy so you don't need to create a exta var for the container.
            _closeTimer = setTimeout(
                    $.proxy(function() {
                        //has the mouse paused
                        close(this);
                    }, this)
                , 750);
    
        });
    
    
        this.addListeners = function() {
            // nothing here
        };
    
        this.init = function() {
            this.addListeners();
        }
    
        // Always call the init?
        this.init();
    
        return this; // Return the new object Test.Navigation
    })();
    

    And use it like

    var nav = new Test.Navigation();
    nav.init();
    

    Also as you can see I upgraded your code a bit. Using $.proxy, _ for private vars.

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

Sidebar

Related Questions

I have modified a working Windows service that had always been starting beforehand. After
I always had trouble with estimating cost/price of finished software (or programming work), so
I am a very new java user, and I have always had trouble with
I have always had the following problem. Sometimes, myEclipse is blocked. So I'm obliged
I always had the notion that when you do unit testing, you have to
I've always had a hard time understanding the full concepts of MVC, MVP, MVVM,
I'm working on a WP7 app and I've had some trouble updating a TextBlock
I've always had trouble with retrieving values in API-returned arrays, usually because they are
We have always had languages that were preferable to be used in a particular
I have always had problem with the root layout. in the paragraph below, you

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.