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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:07:14+00:00 2026-05-24T22:07:14+00:00

I created a gallery with auto-play. When the gallery STOP it’s animation and we

  • 0

I created a gallery with auto-play.

When the gallery STOP it’s animation and we hover the gallery, all works as expected.

But I am getting a strange issue:

While the gallery is animating –> hovering the gallery, the clearTimeout seem not to work correctly and on mouseleave the gallery behave strangely
or it’s happening a strange conflict I cannot understand and resolve. I went back to an other gallery I made a couple of days before fith a similar functionality and I encountered the same issue.

I am missing something crucial here

THE GALLERY IN QUESTION: jsFiddle

var myTimeOut;

/////// ANIMATION /////////////   
    function animation(cb){         
            $('#slider').animate({left: '-=600' },800, cb);
    }      
    /////// AUTO SLIDE ////////////
    function auto() {
        myTimeOut = setTimeout(function() {
            animation(function(){
                auto();                   
            });   
        }, 2000);
    }
    auto();

and how I’m trying to pause it:

    ///// MOUSE actions //////////   
    $('#galcontainer').mouseenter( function () {
      clearTimeout(myTimeOut);
    });
    $('#galcontainer').mouseleave( function () {
     auto();
    });

EDIT

Adding a ‘hover flag’ (as suggested in the answers) works almost great, but a small bug using this solution is visible when ‘fast mouseenter/mouseleave DURING the animation.

A fix to that issue could be adding a $(‘#slider’).stop() at mouseleave. Not a great solution.
What else can I do?

  • 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-24T22:07:14+00:00Added an answer on May 24, 2026 at 10:07 pm

    The issue: on mouseenter during the animation, the jQuery .animate() callback cb is already in queue (even if the clearTimeout was called, another auto() will be called which will trigger subsequent runs, etc.).

    Using setInterval and leveraging all those callbacks is the key:

    jsFiddle demo

    $(function(){                 // DOM ready shorthand
        var $sli =  $("#slider"), // Cache elements you plan to reuse
            galW =  $('#gallery').width(),
            imgN =  $sli.find('img').length,
            counter = 0,          // set it to index 0!
            itv;                  // setInterval (scope var)
        
        $sli.width(galW * imgN);
    
        function anim() {
          ++counter; counter%=imgN;                       // Incr.counter; and wrap-around
          $sli.stop().animate({left: -galW*counter},900); // width*counter = px position
        }
        function autoSlide() {
            itv = setInterval(anim, 3000);
        }
        autoSlide();
        
        ///// MOUSE STATE //////////   
        $sli.hover(function(){ clearInterval(itv); }, autoSlide);
    });
    #galcontainer{
        position:relative;
        margin:20px auto;
        width:600px;
    }
    #gallery{
        position:relative;
        overflow:hidden;
        width:600px;
        height:240px;
    }
    #slider{
        position:absolute;
        top:0px;
        left:0px;
    }
    #slider img{
        float:left;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="galcontainer">    
        <div id="gallery">    
            <div id="slider">
                <img src="http://dummyimage.com/600x240/479/fff"/>
                <img src="http://dummyimage.com/600x240/636/fff"/>
                <img src="http://dummyimage.com/600x240/587/fff"/>
                <img src="http://dummyimage.com/600x240/447/fff"/>
            </div>     
        </div>   
    </div>

    (Aside notes regarding the original (yet modified) code)

    The direction variable with "+=" and "0-" is unneeded since there’s only one direction, so use a counter = 0 indexed, and wrap-around/reset using the Reminder % operator. Also,

            animation(function(){
                auto();                   
            }); 
    

    can be written like:

           animation(auto);
    

    since auto is already a function.

    ///// MOUSE STATE //////////   
    $('#galcontainer').mouseenter( function () {
      clearInterval(myTimeOut);
    });
    $('#galcontainer').mouseleave( function () {
     auto();
    });
    

    can be used with .hover(fn1, fn2) method (like in the above example)

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

Sidebar

Related Questions

I have created a photo gallery application using Three20 framework. It works fine with
I've run into a strange problem here: I created today a mousemove horizontal gallery.
I successfully followed instructions on getting a Three20 photo gallery created. However, when I
I created a gallery using setInterval fiddle The gallery works great, the only problem
Hi I have created one gallery project. but I wish to created folders view
I have an image gallery that I created by reading the contents inside a
I have created a XML image gallery, which displays text in between each slide.
Created .NET WCF service, tested it - works. Generated schemas from Data and service
i have created a gallery script, which first loads images to be shown http://bit.ly/9aQdr3
I have created a gallery, and it displays a lot of images in a

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.