Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

javascript - callback function meaning

What is the meaning of callback function in javascript.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JavaScript's "callback" is function object that can be passed to some other function (like a function pointer or a delegate function), and then called when the function completes, or when there is a need to do so. For example, you can have one main function to which you can pass a function that it will call...

Main function can look like this:

function mainFunc(callBack)
{
    alert("After you click ok, I'll call your callBack");

    //Now lets call the CallBack function
    callBack();
}

You will call it like this:

mainFunc(function(){alert("LALALALALALA ITS CALLBACK!");}

Or:

function thisIsCallback()
{
    alert("LALALALALALA ITS CALLBACK!");
}

mainFunc(thisIsCallback);

This is extensively used in javascript libraries. For example jQuery's animation() function can be passed a function like this to be called when the animation ends.

Passing callback function to some other function doesn't guarantee that it will be called. Executing a callback call (calBack()) totally depends on that function's implementation.

Even the name "call-back" is self-explanatory... =)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...