Ying Yi
Ying Yi

Reputation: 782

Is a best common way to solve button click too fast?

I have a button.when click button, show a dialog to select data.
If click the button so fast,multi dialog will be show.
At present,I have two way to solve this problem
1.use disabled
2.use setTimeout and clearTimeout

have any other better way to solve this problem?
thank you very much

explain:
if use disabled,after dialog close,need to set the button available.
at present,I use this code

Util.prototype.lazyTriggerEvent = function(buttonId,event,callback){
    var searchTrigger=null;
    $("#"+buttonId).bind(event,function(){
        var text = $.trim($(this).val());
        clearTimeout(searchTrigger);
        searchTrigger = setTimeout(function(){
            callback(text);
        },500);
    })
};
//Util.lazyTriggerEvent("showDialgBtnId","click",function(){})

if click button trigger a ajax,and have much more button like this,is a best common way to solve this problem.

Upvotes: 3

Views: 1829

Answers (3)

comwrg
comwrg

Reputation: 41

Use disabled at first, and then when the dialog displays, enable the button.

Upvotes: 0

Derek Story
Derek Story

Reputation: 9583

You can use jquery's .one() handler which limits a function to running once:

JQuery's .one() handler

Description: Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

$('button').one('click', function() {
  // Do stuff
});

Or you can also disable the button on click:

$('button').click(function() {
  $(this).prop('disabled', true);
  // Do stuff
});

To re-enable the button, you can simply add the following to your close modal function:

$('button').prop('disabled', false);

Upvotes: 2

Chiu
Chiu

Reputation: 350

I suppose when you want to show a dialogue, you execute a function called showDialogue() .

In your showDialogue(), you'll be able to check whether your dialogue was initiated.

Keep your mind off the button. Focus on the showDialogue().

If your dialogue was initiated, then do not execute the rest of your code in showDialogue(), as if showDialogue() isn't executed twice. It gives an illusion that the multi click isn't working. Is it the solution you desire, without disable and setTimeout?

Upvotes: 1

Related Questions