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 8826993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:16:38+00:00 2026-06-14T07:16:38+00:00

So I have an element(link) that controls the visibility of another element(target) by hovering

  • 0

So I have an element(link) that controls the visibility of another element(target) by hovering on it.
When the the mouse hovers the link the target is supposed to turn to visible and when the mouse leaves the target is supposed to turn invisible after 2 seconds.

So far so good. But how do I make it so that if the mouse goes hover the visible target before the 2 seconds go up, the target stays visible?

I got it to sort of work with setTimeout and clearTimeout but its really buggy and it doesn’t feel good at all.

var time = 1000;
$(".link").hover(

      function () {
        $('.target').css('display', 'none');
        clearTimeout($(this).data('timeout'));
          $('.target').css({'display': 'block'});
      },

      function () { 

        var timer = setTimeout(function() {$('.target').fadeOut(1000).delay(100).css('display', 'none'); clearTimeout(timer); }, time);

        $('.target').hover(
              function () {
                clearTimeout(timer);
              },
              function () {
                var timer = setTimeout(function() {$('.target').fadeOut(1000).delay(100).css('display', 'none'); clearTimeout(timer); }, time);
              }
            );        
      }

); ​

(http://jsfiddle.net/VfDkq/)

Any help would be greatly appreciated.

Cheers

  • 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-14T07:16:39+00:00Added an answer on June 14, 2026 at 7:16 am

    Try re-factoring your code a bit:

    (function() {
    
        var time = 1000,
            timer;
    
        function handlerIn() {
            clearTimeout(timer);
            $('.target').stop(true).css('opacity', 1).show();
        }
        function handlerOut() {
            timer = setTimeout(function() {
                $('.target').fadeOut(3000);
            }, time);
        }
    
        $(".link, .target").hover(handlerIn, handlerOut);
    
    }());
    

    Live demo

    This should be really close to your described behavior. I’ve also added an extra line to display it right back when you mouseenter while it’s fading out, as you can see in the handlerIn.


    A couple mistakes that I took away from your code:

    • You had another timer var declared inside one of the mouseleave handlers, which couldn’t be cancelled anywhere. The timer must be accessible in a common scope;
    • You were applying animation delays before the synchronous method .css, which has no effect;
    • There’s no point to set the display to none after a fadeOut has completed, it automatically does so;
    • There’s no point to clear a setTimeout after it has executed either.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a link that opens a Thickbox... <a href=page.php?tag=lists-element&keepThis=true&TB_iframe=true&height=450&width=500> Everything works with this
If i have img element id = myimg. Is posible to add link to
I have one function for one element: <a href=javascript:void(0); onclick=dothis()>Link</a> When this function is
I have the following setup: <div>Element with CSS3 animated height change</div> <div><a href=#>Link</a></div> I
I have the following DOM <row> <link href=B&#252;ro.txt target=_blank> my link </link> </row> When
I have an ASP.NET web site which contains some ImageButton controls that cause postbacks
I have some images from another source that need to refresh from their offsite
I have a link to an .mp4 video file that i must embed in
This is a nasty one. I have an external link. <a href=http://www.example.com target=_blank> now
Say I have element tree returned by xquery categories = tree.xpath(u//ul[@class='tree-root']/li) for cat in

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.