jquery disable a button for a specific time

前端 未结 4 834
攒了一身酷
攒了一身酷 2020-12-17 21:34

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

4条回答
  •  生来不讨喜
    2020-12-17 22:04

    Might not be the most elegant solution, but I thought I'd play with jQuery queues on this one...

    ​$.fn.disableFor = function (time) {
        var el = this, qname = 'disqueue';
        el.queue(qname, function () {
            el.attr('disabled', 'disabled');
            setTimeout( function () {
                el.dequeue(qname);
            }, time || 3000);
        })
        .queue(qname, function () {
            el.removeAttr('disabled');
        })
        .dequeue(qname);
    };
    
    $('#btn').click( function () {
        ​$(this).disableFor(2000);​​​​
    });
    

    ​ This is where I worked it out... http://jsfiddle.net/T9QJM/

    And, for reference, How do I chain or queue custom functions using JQuery?

提交回复
热议问题