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

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

Of course, the function isn't called 'call'.

** EDIT **

(Added solution on behalf of the OP).

Solution

http://jsfiddle.net/gx2mJ/1/

or

HTML:

<div id="content">
    <p class="article">
        this is an article
    </p>
</div>

JavaScript:

function callBack() {
    $("#content .article").html("the callback has been called");
}

$(document).ready(function() {
    $("#content .article").slideUp(0);

    $("#content .article").each(function(i) {
        $(this).delay(i*250).slideDown(function(){
             //if ($("#content .article:animated").length < 1) {
                  callBack();
             //}
        });
    });
});
2
  • Why would you want to do that? Maybe there's a better way to do whatever you're trying to do. Commented Oct 11, 2010 at 3:23
  • Meh, just using setTimeout for now. Not the way I wanted to go, but it works. Commented Oct 11, 2010 at 3:45

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

2 Comments

$('#node').slidedown('normal', jsFunc)
well, the anon func at the end allows for actually putting the alert() in it instead of making the function call.
1
$("#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

Comments

0

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);

Comments

0

(Added solution on behalf of the OP).

Solution

http://jsfiddle.net/gx2mJ/1/

or

HTML:

<div id="content">
    <p class="article">
        this is an article
    </p>
</div>

JavaScript:

function callBack() {
    $("#content .article").html("the callback has been called");
}

$(document).ready(function() {
    $("#content .article").slideUp(0);

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

Comments

-1
$("#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 animation triggers have taken place.

4 Comments

Are you answering your own question?
It isn't clear if this is an answer or an addendum to the question. Would you clarify, Jacksonkr? If you do not remember, I would suggest deleting this, since you have another answer that you were happy with (I have moved it from the Q to an answer in its own right).
no, running it on chain will not make it run after animations have taken place.
@Reigel The wording was misleading. I meant animation triggers not animation cycles. Thanks for bringing this to my attention.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.