newbie方法:什么是javascript回调函数? [英] newbie approach: what is a javascript callback function?

查看:339
本文介绍了newbie方法:什么是javascript回调函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个在调用它的其他函数之后执行的函数,完成了吗?



请问我几乎没有关于编程的知识,



我可以从stackoverflow gurus请求尝试吗?

解决方案

一般来说,一旦你调用的另一个函数完成,就使用一个回调函数(就像你在问题中说的那样)。一个很好的例子是AJAX请求:大多数库有一个函数允许你在后台发送请求到服务器,而不刷新页面(这使用AJAX)。您通常为此AJAX函数提供两个回调函数:成功函数和失败函数。



如果此请求成功,它将调用成功函数,做它需要的;例如,它可以刷新页面的一部分,做某种动画,或者提醒用户他们的信息被保存。另一方面,如果它失败,回调函数可能会提醒用户他们的数据没有保存,他们应该再试一次。



回调函数允许库开发人员创建非常通用的代码,别人可以使用,然后根据自己的需要自定义。



下面是一些jQuery代码来展示上面的例子URL不存在):

  jQuery.ajax(
url:'/mywebsite/somepage.php' ,

success:function itWorked(returnedData){
//这是成功函数,只有当ajax调用
//成功完成时才会被调用

alert('Yay it worked!'+ returnedData);
},

错误:function itFailed(originalRequest,textStatus,errorThrown){
//这是错误函数,只有在ajax调用
//时才会调用

alert('It failed!'+ textStatus +'。'+ errorThrown);
}
);

编辑:开始时,我说:一般来说。 。。实际上,回调的使用远不止函数完成时。像在其他答案中所说的,它可以在函数内部的任何地方使用:begin,middle,end。基本思想是代码的开发人员可能不知道你将如何使用HIS代码。所以,他使它非常通用,让你能够做任何你需要的数据。



一个很好的例子是 jQuery.each 方法,它允许您传递将在数组中的每个元素上执行的回调(I说数组,因为它实际上可以迭代许多事情,可能或不是实际的数组)。

 < a href = someurl.html'class ='special_link'>一些网址< / a> 
< a href ='anotherurl.html'class ='special_link'>另一个网址< / a>
< a href ='onelasturl.html'class ='special_link'>最后一个网址< / a>

//下面的代码说明:在每个链接上执行myEachCallbackFunction
//有一个类'special_link'
$('a.special_link')。 function myEachCallbackFunction(i){

//链接变量将包含当前为
//的对象,因此,第一次通过它将保存
//'someurl.html'链接,第二次它将保存
//'anotherurl.html'链接,并且最后一次它将保存
//'onelasturl.html'链接
var link = $(this);

//将每个链接的背景颜色改为红色
link.css('background-color','red') ;
});因此,从这个例子中我们可以看到jQuery的开发人员实现了.each方法,并允许我们使用这个方法来创建一个jQuery。做任何我们想要的每个链接,它被呼吁。


Is just a function that executes after one other function that calls it, finishes?

Please I know (almost) nothing about programming, and I find it quite hard to find a proper newbie answer or explanation about what does this means.

Can I request a try from stackoverflow gurus?

解决方案

In general, a callback function is used once another function you have called finishes (just like you stated in your question). A good example of this is AJAX requests: most libraries have a function that allows you to send a request to the server in the background without refreshing the page (this uses AJAX). You generally provide two callback functions to this AJAX function: a success function, and a failure function.

If this request succeeds, it calls the success function so that your code can do what it needs; for instance, it might refresh part of the page, do some sort of animation, or alert the user that their information was saved. On the other hand, if it failed, the callback function would probably alert the user that their data was not saved and that they should try again.

Callback functions allow library developers to create very generic code that others can use and then customize to their needs.

Below is some jQuery code to show you the example above (this code won't work as the URL doesn't exist):

jQuery.ajax(
  url: '/mywebsite/somepage.php',

  success: function itWorked(returnedData) {
    // This is the success function and will be called only if the ajax call
    // completes succesfully

    alert('Yay it worked! ' + returnedData);
  },

  error: function itFailed(originalRequest, textStatus, errorThrown) {
    // This is the error function and will only be called if the ajax call
    // has an error

    alert('It failed! ' + textStatus + '. ' + errorThrown);
  } 
);

EDIT: At the beginning, I said, "In general...". In reality, callbacks are used for much more than just when a function finishes. Like stated in other answers, it can be used anywhere inside of a function: beginning, middle, end. The basic idea is that the developer of the code may not know how YOU are going to use HIS code. So, he makes it very generic and gives you the ability to do whatever you need with the data.

A good example of this is the jQuery.each method which allows you to pass in a callback that will be executed on each of the elements in the "array" (I say array because it can actually iterate over many things which may or may not be actual arrays).

<a href='someurl.html' class='special_link'>Some URL</a>
<a href='anotherurl.html' class='special_link'>Another URL</a>
<a href='onelasturl.html' class='special_link'>One Last URL</a>

// The following code says: execute the myEachCallbackFunction on every link
// that has a class of 'special_link'
$('a.special_link').each(function myEachCallbackFunction(i) {

  // The link variable will contain the object that is currently
  // being iterated over. So, the first time through, it will hold
  // the 'someurl.html' link, the second time it will hold the
  // 'anotherurl.html' link, and the last time it will hold the
  // 'onelasturl.html' link
  var link = $(this);

  // Change the background color of each link to be red
  link.css('background-color', 'red');
});

So, we can see from this example that the developers of jQuery implemented the .each method and allow us to do whatever we want to to each link that it is called upon.

这篇关于newbie方法:什么是javascript回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆