jQuery防止双击双击

时间:2019-03-30 06:46:41

标签: javascript jquery

我试图防止双击功能两次触发。在同一元素上还有一个单击功能。在大多数情况下,它可以按预期运行,但有时双击或三下会触发两次。

        var toggled=0;
        var toggledId = "";
        var start_btn_el = $('<a id="start_button" class="cursor"></a>');
        $("#can_wrapper").append(start_btn_el);

        $(document).one('dblclick', 'span', function(e){
            e.stopPropagation();
            telnet.gamePrompt(this.id);
            telnet.gamePrompt("1");
            resetUI();
            var loader_el = $('<div class="bar"></div>');
            $("#can_wrapper").append(loader_el);
            $('.bar').center();
        });

        document.querySelectorAll('#start_button')[0].addEventListener('click', function(e){
            e.stopPropagation();
            if ( toggled == 1 ){
                telnet.gamePrompt(toggledId);   
                telnet.gamePrompt("1");
                resetUI();
                var loader_el = $('<div class="bar"></div>');
                $("#can_wrapper").append(loader_el);
                $('.bar').center();
            }
        });

        $(document).on('click', 'div', function(){
            if ( toggled == 1 ){
                // Check if click was triggered on or within #menu_content
                if( $( '#' + toggledId ).data('clicked') ) {
                    return false;
                } else {
                    $( '#' + toggledId ).css({"background-color": "initial","border": "0",});
                    $( '#hov_' + toggledId ).removeClass('player_sel');
                    toggled = 0;
                    toggledId = "";
                }
            }
        });

        $(document).on('click', 'span', function(e){
            if ( toggled == 0 ){
                $( '#' + this.id ).css({"background-color": "#7fffd42e","border": "1px solid #378196",});
                $( '#hov_' + this.id ).addClass('player_sel');
                toggled = 1;
                toggledId = this.id;
                e.stopPropagation();
            }
        });

0 个答案:

没有答案