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

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

问题描述

是否只是一个在调用它的另一个函数完成后执行的函数?

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.

我可以向 stackoverflow 专家请求试用吗?

Can I request a try from stackoverflow gurus?

推荐答案

通常,回调函数会在您调用的另一个函数完成后使用(就像您在问题中所述).一个很好的例子是 AJAX 请求:大多数库都有一个功能,允许您在后台向服务器发送请求而无需刷新页面(这使用 AJAX).您通常会为此 AJAX 函数提供两个回调函数:成功函数和失败函数.

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.

如果这个请求成功,它会调用success函数,这样你的代码就可以做它需要做的事情;例如,它可能会刷新页面的一部分、执行某种动画或提醒用户他们的信息已保存.另一方面,如果失败,回调函数可能会提醒用户他们的数据没有保存,他们应该再试一次.

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.

下面是一些 jQuery 代码,用于向您展示上面的示例(由于 URL 不存在,此代码将不起作用):

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

一开始,我说,一般来说......".实际上,回调不仅仅用于函数完成时.就像其他答案中所述,它可以在函数内部的任何地方使用:开始、中间、结束.基本思想是代码的开发人员可能不知道您将如何使用他的代码.因此,他使它非常通用,并让您能够对数据执行任何您需要的操作.

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.

一个很好的例子是 jQuery.each 方法,它允许您传入回调这将在数组"中的每个元素上执行(我说数组是因为它实际上可以迭代许多可能是也可能不是实际数组的东西).

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

因此,我们可以从这个示例中看到 jQuery 的开发人员实现了 .each 方法,并允许我们对调用它的每个链接做任何我们想做的事情.

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.

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

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