javascript - How to append data only one time after click with jQuery - Stack Overflow

So, i have this little function:carousel_controls_buttons.live('click', function(e){setTimeou

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. When i do this this data appends more than one time and i have content duplication. How can i stop this for some time, f.e. this 450ms? Thx for help.

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. When i do this this data appends more than one time and i have content duplication. How can i stop this for some time, f.e. this 450ms? Thx for help.

Share edited Jan 23, 2013 at 8:59 Tomas Ramirez Sarduy 17.5k8 gold badges72 silver badges86 bronze badges asked Jan 23, 2013 at 8:51 LukasLukas 7,75420 gold badges79 silver badges127 bronze badges 1
  • have a read of Using .one() with .live() jQuery – Adam Tomat Commented Jan 23, 2013 at 8:57
Add a ment  | 

5 Answers 5

Reset to default 3

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:

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/xhSvC/

Note that live method is deprecated, you should use on method instead.

While the other answers ought to work, I would like to introduce you to the concept of debounce & throttle.

http://benalman./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.

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; increment when hit and check for the condition if count==1 append it if not leave it

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);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745097885a4611091.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信