tags:

views:

122

answers:

3

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  A: 

maybe this can help you...

var executing = false;
$('.leftArrow').click(function () {
    if (!executing) {
      executing = true;
      // Execute animation and what not here.
      executing = false;
    } else {
      // just wait?
    }
});
Garis Suero
+1  A: 

It's on you. You should create some boolean logic or, disable / enable the button.

var block = false;
$('.leftArrow').click(function(){
    if(!block){
        block = true;
        // do lots of stuff
        block = false;            
    }
});

It's probably even better to use a callback from an animation method (in jQuery). Like

$('.leftArrow').click(function(){
    var $this = $(this);

    $this.attr('disabled', 'disabled');
    $('somelement').fadeOut('slow', function(){
       $this.removeAttr('disabled');    
    });
});
jAndy
+2  A: 

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.

And don't forget:

To understand recursion, you must first understand recursion.

Chris
Hi Chris,Thanks for your effort to find a solution to my problem. I've tried both methods, and while I understand the logic behind both solutions, neither one seems to work for me. It's like the click() is ignoring the if statement in your first solution and seems to do something similar to the second. I'm posting up more information to hopefully give a better understanding of what's going on.Thanks!
Joel
is it all wrapped in a $(document).ready(function(){ //your code });?
Chris
If you don't have all of your jQuery events (such as your .click()) inside $(document).ready(), it is likely ignoring your .click() trigger. I updated my post above to include it.
Chris
Yeah it is, I've just posted the function that I'm working on.
Joel
I am running your code just fine. I turned "lastTab" in to a string to take care of the "undefined" error. You do have var lastTab defined, correct? Are you getting any errors? Is the animation working the first time?
Chris
I also had to remove the .removeAttr('style'), but that was because I used the style attribute to markup my test HTML.
Chris
Yep, lastTab is defined before the leftArrow function is called. It works perfectly, as long as I don't click the arrow again while the animation and such is taking place. I've added in the rest of the code. Would the HTML and CSS be of use?
Joel
CHRIS! You're a genius! Thank you so much! I have been so close to completion on this and your help has been invaluable! Once I have my site completely up and running, I'll be sure to post you a link to it, if you would like. Thank you, again! :D
Joel
That would be great! I actually had fun on this. It's my type of puzzle :)
Chris