简体   繁体   English

回调函数的含义

[英]callback function meaning

javascript中回调函数的含义是什么?

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. JavaScript的“回调”是可以传递给其他函数(如函数指针或委托函数)的函数对象,然后在函数完成时或需要时调用。 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. 这在javascript库中广泛使用。 For example jQuery's animation() function can be passed a function like this to be called when the animation ends. 例如,jQuery的animation()函数可以传递这样的函数,以便在动画结束时调用。

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. 执行回调调用( calBack() )完全取决于该函数的实现。

Even the name "call-back" is self-explanatory... =) 甚至名称“回叫”也是不言自明的...... =)

It's just a name for a function that should be called back after something. 它只是一个函数的名称,应该在某些东西之后回调。

It's often used with XMLHttpRequest: 它经常与XMLHttpRequest一起使用:

var x = new XMLHttpRequest();
x.onreadystatechange = function(){
    if(x.readyState == 4){
        callbackfunction(x.responseText);
    }
}
x.open('get', 'http://example.com/', true);
x.send(null);

callbackfunction is just a plain function, in this case: callbackfunction函数只是一个普通函数,在这种情况下:

function callbackfunction(text){
    alert("I received: " + text);
}

Besides just being a function, a callback function is an enabler for asynchronous application design. 除了作为一个函数之外,回调函数是异步应用程序设计的推动者。

Instead of calling a function and waiting for the return value(s), potentially locking up the thread or the entire PC or UI on single threaded systems while you wait, with the async pattern you call a function, it returns before finishing, and then you can exit your code (return back to the calling OS, idle state, run loop, whatever...). 在等待时,可能会锁定线程或单线程系统上的整个PC或UI,而不是在调用函数的异步模式下,它会在完成之前返回,然后返回,而不是调用函数并等待返回值你可以退出代码(返回调用操作系统,空闲状态,运行循环,等等......)。 Then later, the OS or asynchronous function calls your code back. 然后,OS或异步函数会调用您的代码。 The code you want it to call back is usually encapsulated in something called a "callback function". 您希望它回调的代码通常封装在称为“回调函数”的内容中。 There are other uses, but this is a common one in OOP UI frameworks. 还有其他用途,但这是OOP UI框架中的常见用途。

With the latest iOS 4.x, you can also use nameless "blocks" for the callback. 使用最新的iOS 4.x,您还可以使用无名“块”进行回调。 But languages that don't have blocks (or anon closures under another name) use functions for function callbacks. 但是没有块的语言(或其他名称下的匿名闭包)使用函数进行函数回调。

You can simply use the below statement onClick event of back button: 您只需使用以下语句后退按钮的onClick事件:

OnClick="javascript:history.go(-1)"

it will work to take you back to your previous page which you had visited recently. 它会帮助您回到最近访问过的上一页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM