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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:32:10+00:00 2026-05-16T06:32:10+00:00

So, I have a function that executes on a .click() event. I want to

  • 0

So, I have a function that executes on a .click() event. I want to know how I can ensure that the function won’t run again before the first run is complete? So, if a user clicks 3 times, the function will run 3 times, but run1 will finish before run2 starts.

I was thinking something like .queue(), but am unsure how to implement it.

$(document).ready(function(){

    var numTabs = 10;
    var lastTab = numTabs;
    var currentClass;
    var newClass;

    // Portfolio Gallery

    $('.leftArrow').click(function () {
        $(this).queue(function () {
            // Moves Main 1-5 across
            for (i=1;i<=4;i++) {
                $('#main'+i).animate({'left': '+=229px'}, 'slow');
                $('#main'+i).removeAttr('style');
            }
            $('#main5').removeAttr('style');

            // Removes the Main Classes
            for (i=1;i<=5;i++) {
                $('#main'+i).removeClass('main'+i);
            }

            // Removes the active class from Main3
            $('#main3').removeClass('active');

            // Sets the New Main 1-5 IDs
            for (i=1;i<=5;i++) {
                currentClass = $('#main'+i).attr('class');
                currentClass = currentClass.substr(2);
                newClass = currentClass - 1;
                if (newClass == 0) {
                    newClass = lastTab;
                }
                $('.ex'+newClass).attr('id', 'main'+i);
            }

            // Sends Old Main 5 to the Stack
            if (newClass == lastTab) {
                newClass = 0;
            }
            $('.ex'+(newClass + 1)).removeAttr('id').addClass('theStack');

            // Reassigns the Main CLASSes
            for (i=1;i<=5;i++) {
                $('#main'+i).addClass('main'+i);
            }

            // Moves New Main 1 from Stack to Main 1 position
            $('#main1').removeClass('theStack');

            //Sets Main 3 as the new focus
            $('#main3').addClass('active');

            $(this).dequeue();
        });
    });
});


<div class="pMainHide">
    <div class="pMainShow">
        <div class="ex1 active main3" id="main3"><a href="images/image1.jpg">Image1</a></div>
        <div class="ex2 main4" id="main4"><a href="images/image2.jpg">Image2</a></div>
        <div class="ex3 main5" id="main5"><a href="images/image3.jpg">Image3</a></div>
        <div class="ex4 theStack"><a href="images/image4.jpg">Image4</a></div>
        <div class="ex5 theStack"><a href="images/image5.jpg">Image5</a></div>
        <div class="ex6 theStack"><a href="images/image6.jpg">Image6</a></div>
        <div class="ex7 theStack"><a href="images/image7.jpg">Image7</a></div>
        <div class="ex8 theStack"><a href="images/image8.jpg">Image8</a></div>
        <div class="ex9 main1" id="main1"><a href="images/image9.jpg">Image9</a></div>
        <div class="ex10 main2" id="main2"><a href="images/image10.jpg">Image10</a></div>
    </div>
</div>

Sorry if this is too big a post, haven’t used Stack Overflow before, and this is my first real attempt at using jQuery.

CSS can be added if needed…

  • 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-16T06:32:11+00:00Added an answer on May 16, 2026 at 6:32 am

    The issue is that each iteration of the animation is on new element. Then the element IDs are changed and the the animation is put in the queue of the new elements. So you run in to a situation where the new elements start the animation in their queue before the previous animation is done. JavaScript/jQuery does not halt the execution of the script during the animation.

    For this, jQuery provides a callback parameter in the .animate() method. The callback is executed after the animation is complete. I tried to take advantage of this to force execution in the desired fashion.

    The solution:

    This still uses the solution I provided below, just implemented in a slightly different way.

    $(document).ready(function(){
    var numTabs = 10;        
    var lastTab = numTabs;        
    var currentClass;        
    var newClass;        
    var count = 0; // counter for myCallback
    var executing = false;
    var myQueue = 0;    
    // Portfolio Gallery        
    
    $('.leftArrow').click(doAnimate); // call a named function
    
    function doAnimate(){
        if(!executing){
            executing = true;         
            // Moves Main 1-5 across  <- you are only moving 1-4 here      
            for (i=1;i<=4;i++) {                    // Add Callback--v    
                $('#main'+i).animate({'left': '+=229px'}, 'slow', myCallback);       
                $('#main'+i).removeAttr('style');        
            }        
            $('#main5').removeAttr('style');        
    
            // Removes the Main Classes        
            for (i=1;i<=5;i++) {        
                $('#main'+i).removeClass('main'+i);        
            }        
    
            // Removes the active class from Main3        
            $('#main3').removeClass('active');        
    
            // Sets the New Main 1-5 IDs        
            for (i=1;i<=5;i++) {        
                currentClass = $('#main'+i).attr('class');        
                currentClass = currentClass.substr(2);        
                newClass = currentClass - 1;        
                if (newClass == 0) {        
                    newClass = lastTab;        
                }        
                $('.ex'+newClass).attr('id', 'main'+i);        
            }        
    
            // Sends Old Main 5 to the Stack        
            if (newClass == lastTab) {        
                newClass = 0;        
            }        
            $('.ex'+(newClass + 1)).removeAttr('id').addClass('theStack');        
    
            // Reassigns the Main CLASSes        
            for (i=1;i<=5;i++) {        
                $('#main'+i).addClass('main'+i);        
            }        
    
            // Moves New Main 1 from Stack to Main 1 position        
            $('#main1').removeClass('theStack');        
    
            //Sets Main 3 as the new focus        
            $('#main3').addClass('active');        
    
        } else {
            myQueue++;
        }   
    }
    function myCallback(){
        count++;
        if(count == 4){ // needs to be the same as your animate for loop
            count = 0;
            executing = false;
            if(myQueue == 0) return;
            myQueue--;
            doAnimate();
        }
    }
    });           
    

    I went with a named function to make it easier to make a call back to the doAnimate() function.

    Previous post below:


    Recursion and some boolean logic will accomplish exactly what you are asking.

    Try this:

    var queue = 0;
    var executing = false; 
    
    $('.leftArrow').click(function () { 
        if (!executing) { 
            executing = true; 
            // Animation 
            executing = false;
            if(queue == 0) return;
            queue--;
            arguments.callee();
        } else { 
            queue++;
        } 
    }); 
    
    • Anytime your animation is running, queue will increment by 1.
    • Anytime queue is not 0, decrement queue, then
    • arguments.callee() will call your anonymous function (this is recursion). Like using $(this), for calling functions.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.