简体   繁体   English

有人能解释一下Callback功能吗?

[英]Can Someone Explain me the Callback functions?

I've started recently to learn about javascript, and I saw a lot of callback functions. 我最近开始学习javascript,我看到了很多回调函数。 What are those functions, why they are used and what for? 这些功能是什么,为什么使用它们以及用于什么? I will be happy to get really basic definition, because I wanna understand it because I realised that it is really important in js. 我很乐意得到真正的基本定义,因为我想理解它,因为我意识到它在js中非常重要。

thanks :) 谢谢 :)

A callback function is a function you pass as an argument to another function. 回调函数是作为参数传递给另一个函数的函数。

The callback function will be called by the function you pass it to (or one further down the chain). 回调函数将由您传递给它的函数(或链中的另一个函数)调用。

This is typically done when the function is doing something asynchronous, so you can't use a return value. 这通常在函数执行异步操作时完成,因此您不能使用返回值。

The main examples are for events: 主要的例子是事件:

// call someFunction when the document is loaded
addEventListener('load', someFunction);

or time related calls: 或时间相关的电话:

// call someFunction after 30 seconds have elapsed
setTimeout(someFunction, 30000);

As we know we can pass different type of variable, object as function's parameter . 我们知道我们可以传递不同类型的变量,object作为函数的参数。 In javascript if a function is passed as parameter then it is called Callback funbction. 在javascript中,如果函数作为参数传递,那么它被称为Callback funbction。

The callback function is called on some event/condition till then the program can execute other code. 在某些事件/条件上调用回调函数,直到程序可以执行其他代码。 The callback function would executed only when the particular event is occurred or particular condition is satisfied. 仅当发生特定事件或满足特定条件时才执行回调函数。

As the name suggests, callback functions are anonymous or named functions that are passed as arguments to another function, or an AJAX call etc. and will be executed after a certain action is completed by the javascript engine. 顾名思义,回调函数是匿名或命名函数,它们作为参数传递给另一个函数或AJAX调用等,并将在javascript引擎完成某个操作后执行。

For eg. 例如。 You can pass a callback function to be executed once an AJAX call has returned with data. 一旦AJAX调用返回数据,您就可以传递一个回调函数。 Ill use jQuery for simplicity : 我简单地使用jQuery:

$.ajax( {
  url: "/my-api-path/",
  data: myParams
}).done(myCallback);

Here, myCallback is a function that will be executed once the AJAX call completes. 这里,myCallback是一个在AJAX调用完成后执行的函数。 The callback function in this case will be called with the response object returned by the AJAX call. 在这种情况下,将使用AJAX调用返回的响应对象调用回调函数。 Notice how this callback has been passed as an argument to the .done method provided by jQuery's AJAX API. 注意这个回调是如何作为jQuery的AJAX API提供的.done方法的参数传递的。

In another example, 在另一个例子中,

setTimeout( 
function() { 
alert("Im inside a callback function!"); 
}, 2000 );

Here the function that contains the alert is the first of the two arguments passed to the setTimeout method in javascript. 这里包含警报的函数是在javascript中传递给setTimeout方法的两个参数中的第一个。 The second being the amount of milliseconds after which this function should be executed. 第二个是应该执行此函数的毫秒数。 Since this function does not have a name it is called an anonymous function. 由于此函数没有名称,因此称为匿名函数。

The same code could be re-written as : 相同的代码可以重写为:

var myCallback = function(){ 
   alert("Im inside a callback");
};
setTimeout(myCallback, 2000);

Callbacks are executed immediately when the action completes. 操作完成后立即执行回调。 So after the engine encounters the setTimeout statement it will store the myCallback function in a reference and then continue execution after the setTimeout statement. 因此,在引擎遇到setTimeout语句后,它会将myCallback函数存储在引用中,然后在setTimeout语句之后继续执行。 Once 2 seconds elapse, it will realise its time to execute the callback so execution will jump to the callback. 一旦经过2秒,它将实现执行回调的时间,因此执行将跳转到回调。 Then the alert will execute, the callback function will terminate and execution will continue back from where it was when 2 seconds elapsed and the engine jumped to the callback. 然后警报将执行,回调函数将终止并且执行将从2秒后的状态继续返回并且引擎跳转到回调。

Hope this explains how callbacks work. 希望这能解释回调是如何工作的。

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

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