简体   繁体   English

如何使用jQuery单击仅一次附加数据

[英]How to append data only one time after click with jQuery

So, i have this little function: 所以,我有这个小功能:

carousel_controls_buttons.live('click', function(e){
    setTimeout(function(){
       info_board_span.append(info_board_description);
       e.preventDefault();
    }, 450);
});

What i'm trying to do is stop appending info_board_description more then one time after two, three fast clicks. 我想做的是在两次,三次快速单击后停止一次附加info_board_description When i do this this data appends more than one time and i have content duplication. 当我执行此操作时,此数据会追加一次以上,并且内容重复。 How can i stop this for some time, fe this 450ms? 我如何才能在450毫秒内停止此操作? Thx for help. 谢谢。

Use a boolean to control it. 使用布尔值来控制它。

var flag = true;
carousel_controls_buttons.live('click', function(e){
    e.preventDefault();
    if (flag) {
       setTimeout(function(){
           info_board_span.append(info_board_description);
           flag = true;
       }, 450);
       flag = false;
    }
});

You can use clearTimeout function: 您可以使用clearTimeout函数:

var t = '';
carousel_controls_buttons.live('click', function(e){
    clearTimeout(t);
    t = setTimeout(function(){
          info_board_span.append(info_board_description);
          e.preventDefault();
    }, 450);
});

example: http://jsfiddle.net/xhSvC/ 示例: http//jsfiddle.net/xhSvC/

Note that live method is deprecated, you should use on method instead. 请注意,不建议使用live方法,而应使用on方法。

While the other answers ought to work, I would like to introduce you to the concept of debounce & throttle . 虽然其他答案应该起作用,但我想向您介绍debouncethrottle的概念。

http://benalman.com/projects/jquery-throttle-debounce-plugin/ is one plugin you may use to achieve what you need, ie, ensure a function is executed only once per x seconds. http://benalman.com/projects/jquery-throttle-debounce-plugin/是您可以用来实现所需功能的一个插件,即确保某个功能每x秒执行一次。

Throttle versus debounce 油门与反跳

Both throttling and debouncing will rate-limit execution of a function, but which is appropriate for a given situation? 节流和反跳都会限制函数的执行速度,但是哪种方法适合给定情况?

Well, to put it simply: while throttling limits the execution of a function to no more than once every delay milliseconds, debouncing guarantees that the function will only ever be executed a single time (given a specified threshhold). 好吧,简单地说:尽管限制将函数的执行限制为每延迟毫秒不超过一次,但反跳保证了该函数将只执行一次(给定阈值)。

carousel_controls_buttons.live('click', function(e) {
    $.debounce(450, function() {
       info_board_span.append(info_board_description);
       e.preventDefault();
    });
});

Put an count over there say var count=0; 把一个数放在那儿说var count = 0; increment when hit and check for the condition if count==1 append it if not leave it 命中时递增,如果count == 1则检查条件,如果不保留则附加条件

There is a one event for achieve this: 有一个事件可以实现这一目标:

carousel_controls_buttons.one('click', function() {
  setTimeout(function(){
       info_board_span.append(info_board_description);
       e.preventDefault();
    }, 450);
});

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

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