Javascript “异步事件”的定义容易理解吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4559032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:56:43  来源:igfitidea点击:

Easy to understand definition of "asynchronous event"?

javascripteventsasynchronousterminologydefinition

提问by jasonbogd

I've encountered this term a lot, and even after Googling, still can't understand what exactly it means. Is there some easy-to-understand (ideally with examples) definition of what an asynchronous event is that someone can provide?

这个词我遇到过很多次,即使在谷歌搜索之后,仍然无法理解它到底是什么意思。对于某人可以提供的异步事件,是否有一些易于理解(最好带有示例)的定义?

Thanks!

谢谢!

回答by epascarello

Non programming example:

非编程示例:

SynchronousYou want a pizza for dinner and you are out of the frozen kind. So you have to stop playing WOW which upsets your guild. You go to the kitchen, make the dough, cover it with sauce, add the cheese, and smother it your favorite bacon topping. You just spent 20 minutes of your time making the pizza with another 10 minutes in the oven. The timer beeps and you pull the hot pie out. You can sit back down in front of your computer, eat the pizza, and continue with your raid.

Synchronous你想要一个比萨当晚餐,你不喜欢冷冻。所以你必须停止玩让你的公会感到不安的 WOW。你去厨房,做面团,涂上酱汁,加入奶酪,然后用你最喜欢的培根浇头把它闷死。您只花了 20 分钟的时间制作比萨饼,而在烤箱中又花了 10 分钟。计时器发出哔哔声,你把热馅饼拿出来。你可以坐在电脑前,吃掉披萨,然后继续你的突袭。

AsynchronousYou want a pizza for dinner while playing WOW. You open up a browser window on your 5th monitor. You load up the Pizza website and order your extra cheesy bacon pizza with a side of bacon grease garlic sauce. You go back to your raid and after 20 minutes the door bell rings. You get the pizza. You sit back down in front of your computer, eat the pizza, and continue with your raid.

异步你想在玩 WOW 的同时晚餐吃披萨。您在第 5 台显示器上打开了一个浏览器窗口。您加载 Pizza 网站并订购带有培根油脂大蒜酱的额外奶酪培根披萨。你回到你的突袭中,20 分钟后门铃响了。你得到披萨。你坐在电脑前,吃掉披萨,然后继续你的突袭。

So what is the difference? One way you waste 20-30 minutes of precious WOW time, the other way you waste $20 plus tip.

那么区别是什么呢?一种方法是浪费 20-30 分钟的宝贵 WOW 时间,另一种方法是浪费 20 美元加小费。

回答by Pointy

Your page is delivered from the server to a client browser, somewhere out there in the Internet. The browser has drawn the page on a screen, and somebody — or some thing— is looking at it. It's a waiting game. Eyes shift back and forth, taking in this or that detail in quick jumps, darting to the side now and then, away from the screen, to investigate distractions in the environment. The clock ticks. The page glows softly, passively, as the user hovers inactive, hand loosely draped over a mouse, neck bent down and eyes more and more intent on something inviting that your page has to offer.

您的页面从服务器传送到客户端浏览器,位于 Internet 的某个地方。浏览器已经在屏幕上绘制了页面,有人或某物正在查看它。这是一个等待游戏。眼睛来回移动,以快速跳跃的方式捕捉这个或那个细节,不时飞到一边,远离屏幕,调查环境中的干扰。时钟滴答作响。当用户不活动地悬停时,页面会轻轻地、被动地发光,手松散地搭在鼠标上,脖子弯下,眼睛越来越专注于您的页面必须提供的一些诱人的东西。

Suddenly, without any warning at all, the cursor begins to move as the hand on the mouse stiffens slightly and begins nudging the little plastic bump over the rough surface of the table. As the mouse moves, its surrogate on the screen moves in close imitation, grazing past interesting images and witty remarks in the content of your page. Eventually a decision is made, the movement pauses, a muscle or two contract slightly, and the mouse button is depressed by an insistent finger. The microswitch in the mouse triggers an electronic impulse, and suddenly the browser is made aware of what's happened: a mouse click.

突然,在没有任何警告的情况下,光标开始移动,鼠标上的手稍微变硬,开始在桌子粗糙的表面上轻推塑料小凸起。当鼠标移动时,它在屏幕上的代理会密切模仿移动,掠过页面内容中有趣的图像和诙谐的评论。最终做出决定,运动暂停,一两块肌肉轻微收缩,鼠标按钮被一个坚持不懈的手指按下。鼠标中的微动开关触发电子脉冲,浏览器突然意识到发生了什么:鼠标点击。

In all that, everything about what the user has done while gazing at the page has happened in a way totally unpredictable to the browser, to any client code in your web page, to anything resident on your servers. There was no knowable "wait time" between human actions. The actions, therefore, as transmitted by the equipment hooked to the user's computer, happened when they happenedand not according to a predictable clock — that is, they happened asynchronously.

总之,用户在注视页面时所做的一切都是以浏览器、网页中的任何客户端代码、驻留在服务器上的任何内容完全不可预测的方式发生的。人类行为之间没有已知的“等待时间”。因此,由连接到用户计算机的设备传输的动作是在它们发生时发生的,而不是根据可预测的时钟发生的——也就是说,它们是异步发生的。

回答by Rob Weir

Think of the end of an interview, and they guy says, "Don't call us, we'll call you". That is the essence of an asynchronous event.

想想面试结束时,他们说,“不要给我们打电话,我们会打电话给你”。这就是异步事件的本质。

Normally you define functions and you call functions explicitly. Your program has a structure where it starts from line 1, then line 2, and except for some conditional code and iterations, calling functions, etc., there is a simple, liner, synchronous structure.

通常,您定义函数并显式调用函数。你的程序有一个结构,它从第 1 行开始,然后是第 2 行,除了一些条件代码和迭代、调用函数等,还有一个简单的、线性的、同步的结构。

But in some cases you have actions that are triggered by events outside of the direct control of the program, things that come from outside the program, like a user interface events (user clicks the mouse) or a network event (someone tries to connect to your server). Your code does not generate these events directly. They are generated outside of your program, often by the OS based on their monitoring of user interface devices and other systems. These are called asynchronous events.

但是在某些情况下,您的操作是由程序直接控制之外的事件触发的,来自程序外部的事物,例如用户界面事件(用户单击鼠标)或网络事件(有人尝试连接到您的服务器)。您的代码不会直接生成这些事件。它们是在您的程序之外生成的,通常是由操作系统根据对用户界面设备和其他系统的监控生成的。这些被称为异步事件。

Just remember, "Don't call us, we'll call you"

请记住,“不要给我们打电话,我们会打电话给你”

回答by Dejan Marjanovic

"In programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing."

“在编程中,异步事件是那些独立于主程序流发生的事件。异步操作是在非阻塞方案中执行的操作,允许主程序流继续处理。”

"With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page."

“使用 Ajax,Web 应用程序可以在后台从服务器异步检索数据,而不会干扰现有页面的显示和行为。”

When you click Edit and Save on SO it is happening asynchronously.

当您单击编辑并保存在 SO 上时,它是异步发生的。

回答by chaimp

An asynchronous event is an event that runs outside the application's main thread.

异步事件是在应用程序主线程之外运行的事件。

The best way to understand is to compare to events that run synchronously. The most typical example would be loading a web page.

最好的理解方法是与同步运行的事件进行比较。最典型的例子是加载网页。

When you went to this page, you clicked on a link and waited for the page to load and were not able to interact with or use this page until it finished loading. To contrast, if this page were to have an AJAX event (that's Asynchronous JavaScript and XML event) associated with some user action, this page would load some data from another source asynchronously - in parallel (theoretically) with any other actions going on.

当您转到此页面时,您单击了一个链接并等待页面加载,并且在加载完成之前无法与此页面交互或使用此页面。相比之下,如果此页面具有与某个用户操作相关联的 AJAX 事件(即异步 JavaScript 和 XML 事件),则此页面将异步加载来自另一个源的一些数据——与正在进行的任何其他操作并行(理论上)。

Example with Two Synchronous Events (A and B): First A does something. When A is finished B does something.

具有两个同步事件(A 和 B)的示例:首先 A 做某事。当 A 完成时 B 做某事。

Example with Two Asynchronous Events (A and B): Both A and B do something at the same time and neither event waits for the other.

两个异步事件(A 和 B)的示例:A 和 B 同时做某事,并且两个事件都不等待另一个。

回答by statenjason

Here's an example of an asynchronous operation in javascript (you need to have your javascript console open)

这是 javascript 中异步操作的示例(您需要打开 javascript 控制台)

console.log('One!');
setTimeout(function(){console.log('Two!');},0);
//Doesn't wait
console.log('Three!');};

//OUTPUT:
//One!
//Three!
//Two!

The call to console.log('Two!')will be executed without blocking the rest of the code that happens after it.

调用console.log('Two!')将在不阻塞其后发生的其余代码的情况下执行。

In a real scenario, replace setTimeout with someone clicking a button on a webpage. The response to the button click will happen eventually, without blocking other code execution such as page rendering.

在实际场景中,将 setTimeout 替换为某人单击网页上的按钮。按钮点击的响应最终会发生,不会阻塞其他代码的执行,比如页面渲染。

回答by Kai

Simply put, it means something that occurs after an unknown amount of time, so don't expect immediate results.

简而言之,它意味着在未知的时间后发生的事情,所以不要指望立竿见影的结果。

For example, "Mom, can I have five dollars?"

例如,“妈妈,我可以要五块钱吗?”

Putting my hand out for money is me expecting her to immediately respond by giving me money (synchronous).

伸手要钱是我希望她立即回应给我钱(同步)。

Realistically, she will look at me for a moment or two, and then decide to respond when she wants to (asynchronous).

实际上,她会看着我一两分钟,然后决定在她想要的时候做出回应(异步)。

回答by Teson

When two different events occur separately from each other, so you can't do

当两个不同的事件彼此分开发生时,你不能这样做

task1
task2

without checking that task1really finished.

没有检查,task1真的完成了。

回答by blackHawk

Asynchronous events are those events that we don't know when it will be occurred in the future for example when server is requested for some file we don't know when it will fulfilled our request or UI events we don't know when user will click on a button or other UI element, but despite of it other things on page or application is happening it does not block anything say the page greyed out all UI until some file from server is not coming or some event is happening all things are independent this is the power of asynchronous events, simply say independent events

异步事件是那些我们不知道将来何时会发生的事件,例如当服务器请求某个文件时我们不知道它何时会满足我们的请求或 UI 事件我们不知道用户何时会单击按钮或其他 UI 元素,但尽管发生了页面或应用程序上的其他事情,但它不会阻止任何内容,例如页面使所有 UI 变灰,直到来自服务器的某些文件未出现或发生某些事件,所有事情都是独立的这就是异步事件的威力,简单来说就是独立事件

回答by Grant Gryczan

If code is synchronous(or sync), it means each piece of code runs in order, sequentially, and the next piece of code cannot run until the previous is completed. Most code is typically synchronous.

如果代码是同步的(或同步),则意味着每段代码按顺序运行,顺序执行,并且下一段代码在前一段代码完成之前无法运行。大多数代码通常是同步的。

If code is asynchronous(or async), it means that code can run separately and independently of other code. If there is async code in the middle of a bunch of sync code, under the context of this particular question, the async code will only run when its event is triggered, regardless of where in the sync code you put it. It is completely separate and independent of the sync code and runs whenever its event says to, not just when the previous piece of code is done running. Some examples of this would be for code that runs on a timed interval, after a file is successfully saved, after a web request is sent, when the user clicks a button, or after an image loads.

如果代码是异步(或异步),则意味着代码可以独立于其他代码单独运行。如果在一堆同步代码中间有异步代码,在这个特定问题的上下文中,异步代码只会在其事件被触发时运行,而不管你把它放在同步代码的什么地方。它完全独立于同步代码,并在其事件要求时运行,而不仅仅是在前一段代码运行完毕时运行。这方面的一些示例适用于按时间间隔运行的代码、成功保存文件后、发送 Web 请求后、用户单击按钮时或图像加载后。