7

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!

4 Answers 4

10

Change line below

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

to

$('.container').delay(2000).fadeIn(callMe)
2
  • @arunes Thank You!.. Is it possible to call a function that was on a separate js file? eg. script.js [$('.container').delay(2000).fadeIn(callMe)] & external.js[functions callMe() {...}]
    – Pennf0lio
    Mar 1, 2012 at 19:19
  • Yes it is possible too. Of course if you add js file which have function callMe to document with <script> tag. Btw if answer helped you, mark as a answered ;)
    – arunes
    Mar 1, 2012 at 19:23
6

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();
  });
});
1

All of the jQuery animation methods have completion callbacks that allow you to run code after animation is done

$('.container').hide().delay(2000).fadeIn(callMe) ;;
0
0
$(function(){

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

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

});

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.