Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perfom action only after animate is finished?

Tags:

jquery

I have my code:

$(this).attr("disabled","disabled");
$(this).animate({'opacity':'0.6'}, 200);
$(this).animate({'opacity':'1'}, 200);
$(this).attr("disabled","");

I want that the input would be disabled when the keypress'ed and it would be enabled only then, when the animation would be executed.

How can I do that?

Edit: I just realized I could do like that: $(this).delay(200).attr("disabled","");. Is it good practice?

like image 509
good_evening Avatar asked Jul 07 '11 17:07

good_evening


2 Answers

Use a callback:

$(this).animate({'opacity':'0.6'}, 200, function() {
    // Animation complete.
  });
like image 139
Mrchief Avatar answered Oct 14 '22 10:10

Mrchief


Not good practice to delay events, it is better to use an animation callback function. For example:

$(this).animate({'opacity':'1'}, 200, function(){
  $(this).attr("disabled","");
});
like image 24
Tak Avatar answered Oct 14 '22 10:10

Tak