alert 回调_JavaScript中到底什么时候回调函数Callback

70bb88fa309f1aad45a9211447cd8927.png

什么是回调函数Callback

简单的理解:回调函数是在另一个函数执行完毕后执行的函数 - 因此名称为'call back'。

复杂的理解:在JavaScript中,函数是对象。因此,函数可以将函数作为参数,并且可以由其他函数返回。执行此操作的函数称为高阶函数。任何作为参数传递的函数都称为回调函数。

为什么需要回调函数Callback

一个非常重要的原因 - JavaScript是一种事件驱动的语言。这意味着,在继续之前,JavaScript不会等待响应,而是在侦听其他事件时继续执行。

看一个基本的例子:

function first(){ console.log(1);}function second(){ console.log(2);}first();second();

正如您所料,首先执行函数,然后执行第二个函数 - 将以下内容记录到控制台:

// 1// 2

但是如果函数首先包含某种无法立即执行的代码呢?例如,我们必须发送请求然后等待响应的API请求?为了模拟这个动作,我们将使用setTimeout,这是一个在一段时间后调用函数的JavaScript函数。我们将函数延迟500毫秒来模拟API请求。我们的新代码如下所示:

function first(){ // Simulate a code delay setTimeout( function(){ console.log(1); }, 500 );}function second(){ console.log(2);}first();second();

了解setTimeout()现在如何工作并不重要。重要的是你看到我们已经移动了我们的console.log(1);在我们的500毫秒延迟之内。那么当我们调用函数时会发生什么呢?

first();second();// 2// 1

即使我们首先调用first()函数,我们也会在second()函数之后注销该函数的结果。并不是JavaScript没有按照我们想要的顺序执行我们的函数,而是在继续执行second()之前,JavaScript没有等待来自first()的响应。

创建一个回调函数Callback

function doHomework(subject) { alert(`Starting my ${subject} homework.`);}

上面,我们创建了doHomework函数。我们的函数接受一个变量,通过在控制台中键入以下内容来调用您的函数:

doHomework('math');// Alerts: Starting my math homework.

现在让我们添加回调 - 作为doHomework()函数中的最后一个参数,我们可以在回调中传递。然后在我们调用doHomework()的第二个参数中定义回调函数。

function doHomework(subject, callback) { alert(`Starting my ${subject} homework.`); callback();}doHomework('math', function() { alert('Finished my homework');});

但是回调函数并不总是必须在函数调用中定义。可以在我们的代码中的其他位置定义

function doHomework(subject, callback) { alert(`Starting my ${subject} homework.`); callback();}function alertFinished(){ alert('Finished my homework');}doHomework('math', alertFinished);

结果是一样的

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442017.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CF#148B】Escape(模拟)

题干: The princess is going to escape the dragons cave, and she needs to plan it carefully. The princess runs at vp miles per hour, and the dragon flies at vd miles per hour. The dragon will discover the escape after t hours and will chase the…

mysql sql语句分页查询_如何用sql语句 实现分页查询?

我制作了一个数据库其中一张表createtablenews(news_idintprimarykeyidentity(1,1),news_titlevarchar(50)notnull,news_authorvarchar(20),news_summaryvarchar(50),news_contenttext...我制作了一个数据库其中一张表create table news(news_id int primary key identity(1,1)…

*【HDU - 1042 】 N! (大数乘法)

题干: Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file. Output For each N, output N! in one line. Sample Input 1 2 3 Sample Output 1 2 6 解题报告: 大数运…

【bzoj 1754】【POJ - 2389 】Bull Math (高精度运算)

题干: Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls answers. Read in two…

mysql居左查询abcd_数据库--查询语句

查询语句mysql中要学习的知识:多表关系,查询语句,索引添加数据补充将一个查询结果插入到另一张表中create table student(name char(10), gender int);insert into student values(nalituo, 1);insert into student values(sasigi, 0);create…

druid mysql配置详解_druid 参数配置详解

spring.datasource.typecom.alibaba.druid.pool.DruidDataSource#驱动配置信息spring.datasource.driver-class-namecom.mysql.jdbc.Driver#基本连接信息spring.datasource.username rootspring.datasource.password rootspring.datasource.urljdbc:mysql://192.168.153.23:3…

【POJ - 2376】Cleaning Shifts (贪心)

题干&#xff1a; Farmer John is assigning some of his N (1 < N < 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 < T < 1,000,000), …

mysql sql 片段_MySQL代码片段

1.[代码][SQL]代码--导出为xml文件mysql -X -uroot -proot -e "use testa;select * from test_tb;" > /opt/test.xml--导出为csv文件--fields terminated 分割记录中每个字段的字符--optionally enclosed 包围每个字段的字符--lines terminated 每行结束的字符--P…

【HDU - 4990】 Reading comprehension (构造+矩阵快速幂)

题干&#xff1a; Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include <cstring> #include <cmath> #include &…

finereport文本框如何实现多值查询_如何实现参数级联查询

参数级联查询是查询控件之间的一种互动方式&#xff0c;比如在某个下拉框选定选项后&#xff0c;另一个下拉框里的选项范围会随之变化。润乾报表提供了多种编辑风格&#xff0c;每种编辑风格都有丰富的属性&#xff0c;以此为基础实现参数级联查询也很简单。下面就通过一个例子…

【HDU - 5015 】233 Matrix (矩阵快速幂)

题干&#xff1a; In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 233…

【HDU - 2899】 Strange fuction(二分或三分,求导)

题干&#xff1a; Now, here is a fuction: F(x) 6 * x^78*x^67*x^35*x^2-y*x (0 < x <100) Can you find the minimum value when x is between 0 and 100. Input The first line of the input contains an integer T(1<T<100) which means the number of…

php mysql html标签_HTML标签格式化PHP和MySQL

我有这个MySQL语句Select type.type, color.color, ShotName, Item.name, Item.Item_idFrom typeInner Join ItemOn type.type_id Item.type_idInner Join colorOn color.color_id Item.color_idWhere Item.state0 And Item.offline 0Group By color.color, Item.name, type.o…

【CF#706B】 Interesting drink (二分)

题干&#xff1a; 瓦西里喜欢在努力工作后休息&#xff0c;所以你可能经常在附近的一些酒吧见到他。他喜欢 "Beecola"&#xff0c;可以从 n 个不同的商店买到。在第 i 个商店的价格为 xi 元。 瓦西里计划购买他最喜欢的饮料 q 次。在第 i 天他能花 mi 元。他想知道每…

mysql datetime month不走索引_like百分号加前面一定不走索引吗?一不小心就翻车,关于mysql索引那些容易错的点...

like百分号加前面一定不走索引吗&#xff1f;正常来讲&#xff0c;我们都知道在mysql的like查询中&#xff0c;百分号加在关键词后面是走索引的&#xff0c;比如 select * like "张三%"&#xff0c;而百分号在前面是不走索引的&#xff0c;比如 select * like "…

ACM竞赛、数论内容常用的定理(求解(a/b)%c,乘法逆元,费马小定理)

如果b与c互素&#xff0c;则(a/b)%ca*b^((c)-1)%c其中是欧拉函数。或者(a/b)%ca*b^(c-2)%c 如果b与c不互素&#xff0c;则(a/b)%c(a%bc)/b 对于b与c互素和不互素都有(a/b)%c(a%bc)/b成立 乘法逆元用扩展欧几里得定理&#xff1a; 例题&#xff1a;ZOJ - 3609 题干&#xf…

自定义菜单url不能带_微服务架构【SpringBoot+SpringCloud+VUE】五 || 实战项目微信公众号自定义开发...

本章主要讲解微信公众号自定义菜单、微信网页开发、模板消息推送等功能的实现&#xff1b;发福利了&#xff0c;下方关注公众号&#xff0c;就能免费获取项目源码1、自定义菜单开发前需要了解以下几点&#xff1a;1、微信公众号的自定义菜单最多包括3个一级菜单&#xff0c;每个…

C语言编程中关于负数的%运算的判定。

如果 % 两边的操作数都为正数&#xff0c;则结果为正数或零&#xff1b;如果 % 两边的操作数都是负数&#xff0c;则结果为负数或零。C99 以前&#xff0c;并没有规定如果操作数中有一方为负数&#xff0c;模除的结果会是什么。C99 规定&#xff0c;如果 % 左边的操作数是正数&…

mnesia mysql性能,Mnesia数据库的存储容量是多少?

Some places state 2GB period. Some places state it depends up the number of nodes.解决方案Quite large if your question is "whats the storage capacity of an mnesia database made up of a huge number of disc_only_copies tables" - youre largely limit…

无数种求逆元的方法总结

乘法逆元 对于缩系中的元素&#xff0c;每个数a均有唯一的与之对应的乘法逆元x&#xff0c;使得ax≡1(mod n) 一个数有逆元的充分必要条件是gcd(a,n)1&#xff0c;此时逆元唯一存在 逆元的含义&#xff1a;模n意义下&#xff0c;1个数a如果有逆元x&#xff0c;那么除以a相当于…