2016-04-24 79 views
0

刚刚添加一些摇动效果,当用户提交表单上的输入不完整。jQuery UI - 防止多次触发Shake效果

但是当用户多次按下提交按钮时,震动效果被多次触发。

我怎样才能防止它被解雇多次?

stop() - 不工作。

clearQueue() - 即使它从它在页面中的第一位移动,也会停止它。

这是我与来:

$(this).effect("shake"); 

谢谢:)

回答

0

如果你可以使用一个额外的变量,你可以使用一个标志:

first_submit = true 

if(first_submit){ 
    first_submit = false 
    $(this).effect("shake"); 
} 
0

您可以创建一个变量设置效果是活动的(正在运行)。当您的提交电话被回调截获时,您会验证它是否属实,如果是,则返回。防止另一个电话。

在效果回调(成功)中,例如,设置变量false。这就是循环。

我增加了一个扩展的回调是成功的效果,请看这里:What is the true way of adding callback functionality to custom JQuery Animation?

喜欢的东西:

// Your new control 
 
var active = false; 
 

 
// Extending callback functionality to the animation 
 
$.fn.shake = function (times, distance, duration, callback) { 
 
    return this.css({ position: 'relative' }).each(function() {    
 
     for (var i = 0, t = duration/times; i < times; i+= 1) { 
 
      $(this). 
 
       animate({ left: -distance }, t/3). 
 
       animate({ left: distance }, t/3). 
 
       animate({ left:   0 }, t/4); 
 
     } 
 

 
     $(this).show(callback); 
 
    }); 
 
}; 
 

 
// DOM load 
 
$(document).ready(function(){ 
 
    
 
    // Triggering form the event 
 
    $('#form_ID').submit({function(){ 
 
    
 
    if(active == true) 
 
     return false; 
 
    
 
    // trigger your event.. 
 
    shake(5, 10, 500, function() { 
 
     $('#your_component_ID').removeClass('shaking'); 
 
    }); 
 
    }); 
 
});

我想你可以拿这个来看看帖子太:jQuery prevent multiple clicks until animation is done