【问题标题】:Disable click on button when clicking multiple times with jquery使用jquery多次单击时禁用单击按钮
【发布时间】:2016-10-05 06:39:55
【问题描述】:

我有一个触发第一次点击和第二次点击的按钮点击。所以当点击多次动画就崩溃了。

有没有办法在动画期间禁用点击并仅在动画完成时启用?

请参阅下面我的代码,其中 prop 已禁用,但无法正常工作

jQuery('.start_button').click(function(event){
    jQuery(this).find('button').prop('disabled', true);
    if(clickcount === 0){
        jQuery(this).find('button').prop('disabled', true);
        wheelForuneAnim.startAnimation();
        jQuery(this).find('button').prop('disabled', false);
        clickcount = 1;
    } 
    else if(clickcount === 1){
        jQuery(this).find('button').prop('disabled', true);
        wheelForuneAnim.stopFull();
        setTimeout(function(){
            wheelForuneAnim.stopAnimation();
        }, 700);
        clickcount = 0;

    }; 
});

这是标记

<div class="start_button">
      <button type="button"></button>  
</div>

方法

startAnimation = function () {
        interval = setInterval(this.animation, 250);
    };
animation = function () {
        if((wheelelement % elems.length)==0){ wheelelement = 0 ;}
        elems.removeClass('active_wheel');
        jQuery('.game_wheel_item:eq('+wheelelement+')').addClass('active_wheel');
        wheelelement++;
    };

任何想法如何解决这个问题?

【问题讨论】:

  • JQuery(this) 是一个 div,里面有一个
  • 所以.start_button 是一个名字很糟糕的div 元素,它包含一个按钮?在这里查看您的实际 HTML 会有所帮助。
  • 查看更新的问题@RoryMcCrossan
  • @Satpal setTimeout 方法本身有回调,我认为这足以解决问题
  • 那么我该如何正确解决这个问题? @Satpal

标签: javascript jquery click


【解决方案1】:

不需要那么多代码,下面的代码解决你的问题

jQuery('.start_button').on('click','button',function(event){
    jQuery(this).off('click','button');

       wheelForuneAnim.startAnimation(function()
       {
            // do your second if stuff
           setTimeout(function(){
                 wheelForuneAnim.stopAnimation(function()
                 {
                       // after function finished executing do some stuff here
                 });
                 jQuery(this).on('click','button');
            }, 700); 
       });
});

你的方法

startAnimation = function (cbck) {
        interval = setInterval(this.animation, 250);
        if(cbck)
        {
           cbck();
        }
    };
stopanimation = function (cbck) {
        if((wheelelement % elems.length)==0){ wheelelement = 0 ;}
        elems.removeClass('active_wheel');
        jQuery('.game_wheel_item:eq('+wheelelement+')').addClass('active_wheel');
        wheelelement++;

       if(cbck)
           cbck();
    };

【讨论】:

  • 为什么是setTimeout
  • 在我的 if 语句中添加和关闭时,它无法正常工作@Keerthivasan
  • 请提供链接(参考),所以功劳归于提供代码的实际用户,只是不要复制和发布答案。
  • @Anahit DEV 为什么还需要 if 语句?
  • 因为我在第一次单击和第二次单击时有 2 个不同的操作。如果你检查我的代码 sn-p 你会看到@Keerthivasan
【解决方案2】:

你总是可以尝试这样做:

jQuery('.start_button button').click(function(event) { //This gets the button in the div

  var button = jQuery(this);

  if (buttonText(button.text()) === "Start") {
    wheelForuneAnim.startAnimation();
    button.text('Stop')
  } else {
    wheelForuneAnim.stopFull();
    setTimeout(function() {
      wheelForuneAnim.stopAnimation();
      button.text('Start')
    }, 700);
  }
})

function buttonText(text) {
  if (text === "Start") {
    return "Start"
  } else {
    return "Stop"
  }
}

这不会禁用您的按钮,但我认为它会帮助您防止动画中断。

【讨论】:

  • 这似乎是相同的情况和问题,禁用按钮的确切位置在哪里。有任何想法吗?我尝试了不同的变体,但仍然没有什么好@Zorken17。此外,它不仅仅是停止,第二次单击同一个按钮时会出现不同的动画,这就是问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
  • 2012-04-07
相关资源
最近更新 更多