good_evening
good_evening

Reputation: 21759

Jquery delay disabled

Why the following code doesn't work? I want that input would be disabled for a one second and then it would go normal again.

$('#chat_bg').attr('disabled', true).delay(1).$('#chat_bg').attr('disabled', false);

Upvotes: 2

Views: 2398

Answers (2)

Felix Kling
Felix Kling

Reputation: 816780

You can use queue [docs] to add a function to (in this case) the fx queue:

$('#chat_bg').prop('disabled', true).delay(1000).queue(function(next) {
    $(this).prop('disabled', false);
    next();
});

delay [docs] takes the time in milliseconds and only effects functions that are work on the fx queue. Most functions are not added to any queue at all, like attr. In this case you have to explicitly put a function in the queue (as shown above).

Also, as you set boolean values, you should use prop [docs] instead.


Or simply use setTimeout, as @Matt describes.

Upvotes: 3

Matt Ball
Matt Ball

Reputation: 359966

Aside from the syntactically invalid JavaScript, and the fact that .delay() interprets the argument as milliseconds, not seconds:

.delay() only affect the events queue by default, which .attr() does not interact with.

Keep it simple, and just use setTimeout.

var $elt = $('#chat_bg').attr('disabled', true);
setTimeout(function ()
{
    $elt.attr('disabled', false);
}, 1000);

Upvotes: 8

Related Questions