views:

57

answers:

5
function jsFunc() {
  alert('i am js!');
}

$('#node').slideDown().call(jsFunc());

Of course, the function isn't called 'call'. Any help?

+2  A: 

You can't do that by default, but you could easily add it as a plugin:

$.fn.call = function (fn, args, thisp) {
    fn.apply(thisp || this, args);
    return this; // if you want to maintain chainability -- other wise, you can move the return up one line..
}

Though I'm not sure why you would want to do that. If you're thinking it won't be run until after the slide is done, then you'd be wrong because jQuery animations are asynchronous.

CD Sanchez
+2  A: 

why not just use a callback function? almost all jquery functions have them.

$('#node').slidedown('normal', function(){jsFunc()})
contagious
`$('#node').slidedown('normal', jsFunc)`
Moak
well, the anon func at the end allows for actually putting the alert() in it instead of making the function call.
contagious
+1  A: 

What is your objective of calling the jsFunc()?

If you want it as a callback you can use the sysntax given here ex:

$('#node').slidedown('normal', function(){jsFunc()}).

But if you want the function jsFunc to be able to call as a plugin, you need to write a plugin as suggested by CD Sanchez.

I think again there is one issue in your sample code, you are calling the function jsFunc and passing the value returned by jsFunc as an argument to the call function. If you want to pass the function jsFunc as the callback function you need to use the syntax

$('#node').slideDown().call(jsFunc);
Arun P Johny
A: 
$("#content article").each(function(i) {
    $(this).delay(i*250).slideDown();
}).callBack();

The whole reason for having the callback in the chain is so it will run AFTER all the animations have taken place.

Jackson
Are you answering your own question?
CD Sanchez
A: 
$("#content article").each(function(i) {
    $(this).delay(i*250).slideDown();
}).callBack();

The whole reason for having the callback in the chain is so it will run AFTER all the animations have taken place.

try this instead,

$("#content .article").each(function(i) {
    $(this).delay(i*250).slideDown(function(){
         if ($("#content .article:animated").length < 1) {
              callBack();
         }
    });
});

the same problem

Reigel