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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:58:39+00:00 2026-06-10T22:58:39+00:00

I’m trying to make elements appear and disappear with fading animation when the user

  • 0

I’m trying to make elements appear and disappear with fading animation when the user clicks. I have this working but there is a problem if the user clicks twice in quick succession or before an animation has finished.
I know the problem will be related to the run loop and the timing of the events and animation etc., but as a newbie to html/js I’m not experienced enough with these issues in that context to know the best solution.

Here’s a fiddle, if you click on the first line and wait a few seconds between each click it’ll work, if you click furiously you’ll soon start to see problems.

Thanks

http://jsfiddle.net/mungbeans/wqUgC/

  • 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-10T22:58:41+00:00Added an answer on June 10, 2026 at 10:58 pm

    This is a more common issue than you might think. The issue is that you have an event set up to set your element to display: none. But if you click again, you want to stop that event from taking place. So what you need to do is keep a reference to the event (setTimeout() returns this) so that you can call clearInterval() on that reference if you need to cancel the hiding of the element:

    var listener = (function() {
        var interval = 0; //A variable to retain the interval            
    
        return function() { //return the original function into listener
            var title = document.getElementById('title');
    
            //dismiss any plans to hide/show the element because we've changed our minds
            // (if the event already fired, this does nothing, which is fine):
            clearInterval(interval); 
    
            if (title.dataset.displayed == 'yes') {
                title.dataset.displayed = 'no';
                setTimeout(function () {
                title.style.webkitTransition = "opacity 2.0s ease";
                title.style.opacity = 0.0;
                }, 0);
    
                //don't forget to hold onto the interval!
                interval = setTimeout(function() { title.style.display = 'none'; }, 2000);
            }
            else {
                title.dataset.displayed = 'yes';
                title.style.display = 'block';
    
                //It's probably not necessary to retain the interval here. 
                // But it doesn't hurt anything.
                interval = setTimeout(function () {
                    title.style.webkitTransition = "opacity 2.0s ease";
                    title.style.opacity = 1.0;
                }, 0);
            }
        };
    }());
    
    
    var li = document.getElementById('list');
    li.addEventListener("click", listener, false);
    

    Note that I wrapped your listener function inside a larger, immediately invoked function expression that returns the original function. I did this so that interval is retained from one call of listener() to the next. You could also make interval a global variable, but those are naughty and this solution is much more pleasantly self-contained.

    Seems to work fine when I swap it into your JSFiddle. Good job with the rest of the code!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I am trying to loop through a bunch of documents I have to put
I need to clean up various Word 'smart' characters in user input, including but

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.