Jquery - Allow the function to run only when it is not running

Asked

Viewed 36 times

2

I’m having a hard time finding a way to block multiples toggles in my code. That is when the client clicks several times in the toggle field, the field keeps appearing and disappearing several times. I wonder how I can make him do it open up or close the field only when the action is not being executed.

Code that makes Toogle:

<script>
(document).ready(function(){
   $(".botao").click(function(){
   $(this).children( ".classe" ).slideToggle();
   });
});
</script>

From now on Thank you!

1 answer

1

The slideToggle has a configuration parameter that does this.

If you use .slideToggle({queue: false}); He will no longer accumulate instructions. Oddly enough, if you press it too fast seems to have a bug and crashes... :/

Thus, as a second option it uses the callback complete. Thus:

$(document).ready(function () {
    $(".botao").click(function () {
        var $this = $(this);
        var animando = $this.data('animando');
        if (!animando) $this.children(".classe").slideToggle(function(){
            $this.data('animando', false);
        });
        $this.data('animando', true);
    });
});

jsFiddle: http://jsfiddle.net/2t25qLhw/1/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.