Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i want to disable a button for a specific time. how can i do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
845 views
Welcome To Ask or Share your Answers For Others

1 Answer

Since this is likely to be a task you might like to repeat, I think the best way to do this would be to extend jQuery like so:

$.fn.timedDisable = function(time) {
    if (time == null) { time = 5000; }
    return $(this).each(function() {
        $(this).attr('disabled', 'disabled');
        var disabledElem = $(this);
        setTimeout(function() {
            disabledElem.removeAttr('disabled');
        }, time);
    });
};

This will allow you to call a function on a set of matched elements which will temporarily disable them. As it is written, you can simply call the function, and the selected elements will be disabled for 5 seconds. You would do that like so:

$('#some-button').timedDisable();

You can adjust the default time setting by changing the 5000 in the following line:

if (time == null) { time = 5000; }

You can optionally pass in a time value in milliseconds to control how long the elements will be disabled for. For example:

$('#some-button').timedDisable(1000);

Here's a working demo: http://jsfiddle.net/fG2ES/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...