Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i chain custom function after a delay?

How do i chain custom function after a delay has been set

here's what i mean: http://jsbin.com/uluyim

$(function(){

  $('.container').hide();

  $('.container').delay(2000).fadeIn().$(document).callMe();   

function callMe () {
  alert ("It works!");

} 

});

Thanks!

like image 852
Pennf0lio Avatar asked Mar 01 '12 19:03

Pennf0lio


2 Answers

Change line below

$('.container').delay(2000).fadeIn().$(document).callMe();   

to

$('.container').delay(2000).fadeIn(callMe)
like image 70
arunes Avatar answered Sep 20 '22 13:09

arunes


In your case, you can use arunes's solution, using the callback from the animation. However, if you need to add a delay between the animation and the callback that gets executed, you can do that too, as in:

$('.container').fadeIn(function () {
  $(this).delay(2000).queue(function () {
    alert('Custom function executed two seconds after fadeIn()!');
    $(this).dequeue();
  });
});
like image 23
schreifels Avatar answered Sep 20 '22 13:09

schreifels