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

发布于 2024-10-09 08:37:03 字数 83 浏览 4 评论 0原文

我经常遇到这个术语,即使在谷歌搜索后,仍然无法理解它到底意味着什么。是否有人可以提供一些易于理解(最好带有示例)的异步事件定义?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

春风十里 2024-10-16 08:37:03

非编程示例:

同步
你晚餐想吃披萨,但你不适合冷冻披萨。所以你必须停止玩WOW,这会让你的公会不高兴。你去厨房,制作面团,涂上酱汁,加入奶酪,然后将其闷在你最喜欢的培根配料上。你只花了 20 分钟制作披萨,另外在烤箱里花了 10 分钟。计时器发出蜂鸣声,你把热馅饼拿出来。你可以坐在电脑前,吃披萨,然后继续你的突袭。

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

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

Non programming example:

Synchronous
You 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.

Asynchronous
You 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.

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

雨落□心尘 2024-10-16 08:37:03

您的页面从服务器传送到位于互联网上某个位置的客户端浏览器。浏览器已在屏幕上绘制了页面,并且有人(或某些事物)正在查看它。这是一场等待的游戏。眼睛来回移动,快速跳跃地捕捉这个或那个细节,时不时地从屏幕上跳到一边,以调查环境中的干扰因素。时钟滴答作响。当用户悬停在非活动状态时,页面会发出柔和、被动的光芒,手松松地搭在鼠标上,脖子低下,眼睛越来越专注于页面提供的诱人内容。

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

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

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.

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 happened and not according to a predictable clock — that is, they happened asynchronously.

纸短情长 2024-10-16 08:37:03

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

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

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

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

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.

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"

铜锣湾横着走 2024-10-16 08:37:03

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

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

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

"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."

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

怎樣才叫好 2024-10-16 08:37:03

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

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

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

两个同步事件(A 和 B)的示例:
首先A做了某事。当 A 完成后,B 会做某事。

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

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.

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

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.

你如我软肋 2024-10-16 08:37:03

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

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

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

console.log('Two!') 的调用将被执行,而不会阻塞发生的其余代码在它之后。

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

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

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.

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.

时光倒影 2024-10-16 08:37:03

简而言之,这意味着在未知的时间之后发生的事情,所以不要期望立即得到结果。

例如,“妈妈,能给我五块钱吗?”

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

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

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).

榆西 2024-10-16 08:37:03

当两个不同的事件彼此分开发生时,因此您不能不

task1
task2

检查 task1 是否确实完成。

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

task1
task2

without checking that task1 really finished.

酒儿 2024-10-16 08:37:03

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

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

ペ泪落弦音 2024-10-16 08:37:03

同步与。异步事件

某些事件处理程序会在事件发生时立即调用。这些称为“同步”事件。一个例子是 DocumentNew。一旦用户创建新文档,它就会被调用。

然而,有些事件在事件发生后不久就会被调用,通常是在短暂的空闲时间之后。这些称为“异步”事件。它们是异步的,因为如果在事件发生的确切时间调用用户编写的宏,则会破坏 Source Insight 的稳定性。

Synchronous Vs. Asynchronous Events

Some event handlers are called immediately when the event occurs. These are called ‘synchronous’ events. An example is DocumentNew. It gets called as soon as the user creates a new document.

However, some events are called shortly after the event occurs, usually after a short amount of idle time. These are called ‘asynchronous’ events. They are asynchronous because it would destabalize Source Insight if a user-written macro were to be called at the exact time the event occured.

绮烟 2024-10-16 08:37:03

如果代码是同步(或同步),则意味着每段代码都按顺序运行,只有前一段代码完成后,下一段代码才能运行。大多数代码通常是同步的。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文