简体   繁体   English

点击Jquery动画,停止多次重复

[英]Jquery Animation on click, stop it repeating on multiple clicks

I've got code like this: 我有这样的代码:

$("#ajax_stuff").load("/site/method", $('.myselector').serializeArray()).delay(100).fadeOut('slow').delay(20).fadeIn('slow');

And basically, that is triggered each time someone clicks a checkbox in a list of checkboxes. 基本上,每当有人单击复选框列表中的复选框时,就会触发该事件。 What happens is if someone quickly clicks 20 checkboxes in a row, the effect repeats 20 times. 如果某人连续快速单击20个复选框,则结果将重复20次。

Ideally i just want it to do one effect for the last box they clicked and then stop. 理想情况下,我只希望它对他们单击并停止的最后一个框产生一种效果。

I've tried 我试过了

$("#ajax_stuff").load("/site/method", $('.myselector').serializeArray()).stop(true,true).delay(100).fadeOut('slow').delay(20).fadeIn('slow');

But that doesn't seem to do anything. 但这似乎无济于事。 Any advice would be very gratefully received! 任何建议将不胜感激!

var delay = (function () {
 var timer = 0;
 return function(callback, ms){
 clearTimeout (timer);
 timer = setTimeout(callback, ms);
      };
    })();
$(function(){
    $("/*CHECKBOX SELECTOR*/").on("click",function(){
        delay((function(){
                      $("#ajax_stuff").load("/site/method", $('.myselector').serializeArray()).stop(true,true).delay(100).fadeOut('slow').delay(20).fadeIn('slow');}),1000);
});
});

setTimeout is your friend in this case, I think. 我认为setTimeout在这种情况下是您的朋友。 I haven't used it on checkbox clicks, but you might have to changed the event handler from click to select or something. 我没有在复选框单击时使用它,但是您可能不得不将事件处理程序从单击更改为选择或其他操作。 I tried to put it in jsfiddle just now, but it's crashed maybe? 我刚刚尝试将其放在jsfiddle ,但是它可能崩溃了? Not loading regardless. 不加载不管。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM