Prevent click event in jQuery triggering multiple times

后端 未结 6 600
甜味超标
甜味超标 2020-12-10 15:01

I have created a jQuery content switcher. Generally, it works fine, but there is one problem with it. If you click the links on the side multiple times, multiple pieces of c

相关标签:
6条回答
  • 2020-12-10 15:23

    One way to do this to use timeStamp property of event like this to gap some time between multiple clicks:

    var a = $("a"),
        stopClick = 0;
    
    
    a.on("click", function(e) {
      if(e.timeStamp - stopClick > 300) { // give 300ms gap between clicks
         // logic here
    
        stopClick = e.timeStamp; // new timestamp given
      }
    
    
    });
    
    0 讨论(0)
  • 2020-12-10 15:31

    Add a variable to use as a lock rather than is(:animating).

    On the click, check if the lock is set. If not, set the lock, start the process, then release the lock when the fadeIn finishes.

    var blockAnimation = false;
    
    $('#tab-list li a').click(
        function() {
            if(blockAnimation != true){
            blockAnimation = true;
            var targetTab = $(this).attr('href');
            if ($(targetTab).is(':hidden')) {
                $('#tab-list li').removeClass('selected');
                var targetTabLink = $(this).parents('li').eq(0);
                $(targetTabLink).addClass('selected');
                $('.tab:visible').fadeOut('slow',
                    function() {
                        $(targetTab).fadeIn('slow', function(){ blockAnimation=false; });
                    }
                );
            }
            }
            return false;
        }
    );
    
    0 讨论(0)
  • One idea would be to remove the click event at the start of your function, and then add the click event back in when your animation has finished, so clicks during the duration would have no effect.

    If you have the ability to execute code when the animation has finished this should work.

    0 讨论(0)
  • 2020-12-10 15:41

    one way would be this:

    $('#tab-list ul li').one( 'click', loadPage );
    var loadPage = function(event) {
        var $this = $(this);
        $global_just_clicked = $this;
        var urlToLoad = $this.attr('href');
        $('#content-area').load( urlToLoad, pageLoaded );
    }
    $global_just_clicked = null;
    var pageLoaded() {
        $global_just_clicked.one( 'click', loadPage );
    }
    

    As you can see, this method is fraught with shortcomings: what happens when another tab is clicked before the current page loads? What if the request is denied? what if its a full moon?

    The answer is: this method is just a rudimentary demonstration. A proper implementation would:

    1. not contain the global variable $global_just_clicked
    2. not rely on .load(). Would use .ajax(), and handle request cancellation, clicking of other tabs etc.

    NOTE: In most cases you need not take this round-about approach. I'm sure you can remedy you code in such a way that multiple clicks to the same tab would not affect the end result.

    jrh.

    0 讨论(0)
  • 2020-12-10 15:42

    Well this is how i did it, and it worked fine.

    $(document).ready(function() {
        $(".clickitey").click(function () {
            if($("#mdpane:animated").length == 0) {
                $("#mdpane").slideToggle("slow"); 
                $(".jcrtarrow").toggleClass("arrow-open");
            }
        });
    });
    

    this is not doing what your code does ofcourse this is a code from my site, but i just like to point how i ignored the clicks that were happening during the animation. Please let me know if this is inefficient in anyway. Thank you.

    0 讨论(0)
  • 2020-12-10 15:43

    I toyed around with the code earlier and came up with the following modification which seems to work:

    $('#tab-list li a').click(
        function() {
            $('.tab:animated').stop(true, true);
            var targetTab = $(this).attr('href');
            if ($(targetTab).is(':hidden')) {
                $('#tab-list li').removeClass('selected');
                var targetTabLink = $(this).parents('li').eq(0);
                $(targetTabLink).addClass('selected');
                $('.tab:visible').fadeOut('slow',
                    function() {
                        $(targetTab).fadeIn('slow');
                    }
                );
            }
            return false;
        }
    );
    

    All that happens is, when a new tab is clicked, it immediately brings the current animation to the end and then begins the new transition.

    0 讨论(0)
提交回复
热议问题