如果在ajax调用期间显示了警报窗口,会发生什么?

时间:2022-02-19 05:54:40

I was just wondering what could happen if, while an ajax call is being executed, an alert is prompted to the user, in the browser window.

我只是想知道如果一个ajax调用被执行,会发生什么,在浏览器窗口中会提示用户。

Let's say, for example, that I have an ajax call

比方说,我有一个ajax调用

$.ajax({
    url: ...,
    type: GET/POST,
    ...
    success: function(data){
        console.log(data);
    },
    error: ...
});

that takes long time to complete (10 sec). While the call is executed, a simple javascript alert is thrown

这需要很长时间才能完成(10秒)。在执行调用时,抛出一个简单的javascript警报

alert("hello!");

What happens for example if:

如果:

  1. ajax call starts
  2. ajax调用开始
  3. ajax call fetching data
  4. ajax调用获取数据
  5. alert is shown
  6. 报警显示
  7. ajax call returns data (alert window is still open!)
  8. ajax调用返回数据(警报窗口仍然打开!)

Knowing that JS is single threaded I know that the script execution will halt, I was just wondering what happens to the ajax call/response if the alert window is not closed "in time".

知道JS是单线程的,我知道脚本执行会停止,我只是想知道如果警告窗口没有“及时”关闭,ajax调用/响应会发生什么变化。

I hope I was clear enough and that this is not a "dummy" question. Thank you

我希望我说得足够清楚,这不是一个“假”问题。谢谢你!

2 个解决方案

#1


60  

This isn't exactly hard to try and see... but the answer is that the alert will take priority and hold up the flow of execution.

这不难看出……但答案是,警报将优先考虑并阻止执行的流程。

Upon closing the alert, and assuming the AJAX request has completed while the alert was open, then the success function will be processed (or error function).

在关闭警报时,假设在警报打开时AJAX请求已经完成,那么将处理成功函数(或错误函数)。

Note that the AJAX request will continue to run while the alert is shown (so a long running AJAX can process while the alert is open), but it is just that your script cannot continue handling the request until the alert is closed.

请注意,在显示警报时,AJAX请求将继续运行(因此长时间运行的AJAX可以在警报打开时进行处理),但您的脚本只能在警报关闭之前继续处理请求。

Here is a working example, notice that the data isn't written to the console until the alert is closed.

这里有一个工作示例,注意在警报关闭之前,数据不会写入控制台。

Another point to be aware of is that after the alert closes, the script will continue with the rest of the function (the code immediate after the alert) before the AJAX response is handled. This helps demonstrate what I mean

要注意的另一点是,在警报关闭之后,在处理AJAX响应之前,脚本将继续执行其余的函数(警报后立即执行的代码)。这有助于说明我的意思。

#2


11  

The HTTP request will continue to run and be processed in the background.

HTTP请求将继续运行并在后台处理。

When the JS event loop becomes free, the readystatechange handler will fire.

当JS事件循环变为空闲时,readystatechange处理程序将启动。

Some intermediate ready states may be skipped because the event loop was busy while that state was true.

某些中间就绪状态可以被跳过,因为事件循环是繁忙的,而该状态是真实的。

#1


60  

This isn't exactly hard to try and see... but the answer is that the alert will take priority and hold up the flow of execution.

这不难看出……但答案是,警报将优先考虑并阻止执行的流程。

Upon closing the alert, and assuming the AJAX request has completed while the alert was open, then the success function will be processed (or error function).

在关闭警报时,假设在警报打开时AJAX请求已经完成,那么将处理成功函数(或错误函数)。

Note that the AJAX request will continue to run while the alert is shown (so a long running AJAX can process while the alert is open), but it is just that your script cannot continue handling the request until the alert is closed.

请注意,在显示警报时,AJAX请求将继续运行(因此长时间运行的AJAX可以在警报打开时进行处理),但您的脚本只能在警报关闭之前继续处理请求。

Here is a working example, notice that the data isn't written to the console until the alert is closed.

这里有一个工作示例,注意在警报关闭之前,数据不会写入控制台。

Another point to be aware of is that after the alert closes, the script will continue with the rest of the function (the code immediate after the alert) before the AJAX response is handled. This helps demonstrate what I mean

要注意的另一点是,在警报关闭之后,在处理AJAX响应之前,脚本将继续执行其余的函数(警报后立即执行的代码)。这有助于说明我的意思。

#2


11  

The HTTP request will continue to run and be processed in the background.

HTTP请求将继续运行并在后台处理。

When the JS event loop becomes free, the readystatechange handler will fire.

当JS事件循环变为空闲时,readystatechange处理程序将启动。

Some intermediate ready states may be skipped because the event loop was busy while that state was true.

某些中间就绪状态可以被跳过,因为事件循环是繁忙的,而该状态是真实的。