防止在元素上触发新事件,直到当前事件函数在jQuery中完成
Prevent new events be triggered on an element until currently event function finishes in jQuery
考虑以下内容:
$('#theElement').on('click',function(){
$(this).animate(...);
$(this).doThis(...);
$(this).doThat(...);
$('anotherElement').animate(...);
$('anotherElement').doThis(...);
$('anotherElement').doThat(...);
});
正如您所看到的,这里是onClick事件的一个简单委托函数。现在,如何在执行该函数的过程中,在"#theElement"上不触发其他事件?
我尝试使用preventDefualt(),但它停止了整个执行,这意味着animate()、doThis()等也不会运行。
在函数启动时在元素上设置布尔标志。
$('#theElement').on('click',function(){
$(this).data('flagname',true);
// ...
在其他事件中测试标志。
if (!$(this).data('flagname')) { // !(undefined) is true
// run code
}
动画完成后清除标志。
var $this = $(this); // 'this' is locally scoped
$this.animate(/* ... */, function() {
$this.data('flagname',false);
});
你必须像这样嵌套它们:
$(".yourclass").animate({}, time, function(){
$(".yoursecondclass").animate({}, time, function(){
});
});
相关文章:
- 无法在java脚本中调用图像的点击事件函数
- 如何在事件函数中访问窗口实例
- 事件函数完整日历中的多个ajax调用
- 如何使参数在事件函数中工作
- Jquery Onclick 事件函数第二次不起作用
- 宣传单:双击时不要触发点击事件函数
- Bootstrap/JQuery-单选按钮组事件函数
- 将jQuery事件函数应用于新的Knockout.js数组元素
- 防止在元素上触发新事件,直到当前事件函数在jQuery中完成
- 有没有一种方法可以将多个参数传递给主干js中的事件函数
- 用javascript更新事件函数中的全局变量
- Jquery:如何在事件函数之外获取变量
- 将参数传递给事件函数
- postMessage - 多个 postMessage 事件/函数/回调
- 多事件函数中的悬停延迟
- JavaScript 事件函数 - 在声明之前调用
- 使用 jQuery 循环设置事件函数触发器
- 暂停,直到事件/函数使用 jquery 或 JavaScript 完成
- 如何访问 jQuery 事件函数中的对象属性
- 单击事件函数未从花式盒子灯箱内触发