【问题标题】:Javascript "Don´t change states to fast"Javascript“不要将状态更改为快速”
【发布时间】:2018-06-14 12:01:07
【问题描述】:

嘿,伙计们,我有一个代码,当您单击按钮时,该代码会从一种状态变为另一种状态(它会启动视频并混合)。现在我试着说我的事件处理程序,如果有人点击得很快,他不应该把它用作输入。像 = Eventhandler 之类的东西,请注意一秒钟内只需单击一次。希望这是我的代码可以理解的:

<script>
    var clickState = 0;
    var btn = document.querySelector('#playbutton');

    btn.addEventListener('click', function(){

    if (clickState == 0) {
    document.querySelector('#toggler').emit('fade_1');

    var videoEl_1 = document.querySelector('#video');

    videoEl_1.play();
    document.querySelector( "#skyid" ).emit('fade_1');
    clickState = 1;

    } else {

    document.querySelector('#toggler').emit('fade_2');

    var videoEl_1 = document.querySelector('#video');

    videoEl_1.pause();
    document.querySelector( "#skyid" ).emit('fade_2');
    clickState = 0;
      }
    });
</script>

【问题讨论】:

    标签: javascript button event-handling toggle eventhandler


    【解决方案1】:

    您可以在单击按钮时禁用该按钮,然后设置超时以在一秒钟后重新启用它。

    这样

    $( document ).ready(function() {
        $("#myButton").click(function(){
            // disable the button
            $("#myButton").prop("disabled", true);
    
            //do the things you want the button to do:
            console.log("doing stuff");
    
            // reenable the button after 1 second
            setTimeout(function(){
                $("#myButton").prop("disabled", false);
            }, 1000);
        });
    });
    

    这里的例子:

    https://jsfiddle.net/20n1gb89/8/

    我在这里使用了一些 jQuery,但 setTimeout 是原生 JavaScript

    编辑:

    您似乎在同一个按钮的单击处理程序中定义了一个单击处理程序。看我的cmets。删除 btn.addEventListener 并保留 if else 语句。看看这是否有效。

     $(document).ready(function () {
            // here you define a click handler for playbutton
            $("#playbutton").click(function () {
                // disable the button
                $("#playbutton").prop("disabled", true);
    
                //do the things you want the button to do:
                var clickState = 0;
                var btn = document.querySelector('#playbutton');
    
                // here you define a click handler for the same button 
                // inside the first click handler. You shouldn't do that.
                btn.addEventListener('click', function () {
                    if (clickState == 0) {
                        document.querySelector('#toggler').emit('fade_1');
    
                        var videoEl_1 = document.querySelector('#video');
    
                        videoEl_1.play();
                        document.querySelector("#skyid").emit('fade_1');
                        clickState = 1;
                    } else {
                        document.querySelector('#toggler').emit('fade_2');
    
                        var videoEl_1 = document.querySelector('#video');
    
                        videoEl_1.pause();
                        document.querySelector("#skyid").emit('fade_2');
                        clickState = 0;
                    }
    
                    console.log("doing stuff");
    
                    // reenable the button after 1 second
                    setTimeout(function () {
                        $("#playbutton").prop("disabled", false);
                    }, 2000);
                });
    
            });
        });
    

    编辑 2:

    就是这样。试试这个:

    $(document).ready(function () {
        $("#playbutton").click(function () {
            // disable the button
            $("#playbutton").prop("disabled", true);
    
            //do the things you want the button to do:
    
            var clickState = 0;
    
            // this doesn't really make sense. clickState will always be 0 
            // as it is defined as 0 each time you click the button. You 
            // will need to define clickState outside the click handler 
            // for this to work.
            if (clickState == 0) {
                document.querySelector('#toggler').emit('fade_1');
    
                var videoEl_1 = document.querySelector('#video');
    
                videoEl_1.play();
                document.querySelector("#skyid").emit('fade_1');
                clickState = 1;
            } else {
               document.querySelector('#toggler').emit('fade_2');
    
                var videoEl_1 = document.querySelector('#video');
    
                videoEl_1.pause();
                document.querySelector("#skyid").emit('fade_2');
                clickState = 0;
            }
    
            console.log("doing stuff");
    
            // reenable the button after 1 second
            setTimeout(function () {
                $("#playbutton").prop("disabled", false);
            }, 2000);
        });
    
    });
    

    });

    【讨论】:

    • 嗯我尝试将我的代码插入到 `doing stuff* 但它想要正常工作一些想法?
    • 没有错误,它似乎有点工作。你不能快速按下,但第一次点击不会被注意到,有时它仍然可以在超时结束之前被点击
    • 它可以工作,但不会经常有点混乱,就像当你按 5 次或其他东西时它会跳转 thx fpr 帮助我确定这个结果我不认为有人连续快速按 5 次
    • 现在一切都很好 :) 也许我只需要删除缓存或其他一些东西
    【解决方案2】:

    您可以在点击时添加事件监听器并使用它创建计时器

    例如,这里用户点击按钮后,我们启动一个1000ms的定时器,如果在这个时间间隔内点击了按钮,我们会显示警报。

     var btn = document.querySelector('#playbutton');
     btn.addEventListener('click', function(){
           setTimeout(function(){
                 if (btn.clicked == true){
                      alert("Cmon dude, chill a little");
                 }
           }, 1000);
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      相关资源
      最近更新 更多