Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
802 views
in Technique[技术] by (71.8m points)

javascript - can a synchronous (blocking) ajax call block the browser's UI?

This question is jQuery oriented, but not necessarily exclusively.

The short question:

Can synchronous ajax call block a normal button from being clicked on? My testings show it doesn't happen, but maybe another browser makes problems.

The long question:

In another question I've asked, how to block on ajax call (I want it to block), the guys said that the browser will block in some cases.

Actually even the docs of jQuery say: Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

I'm trying to:
1. Understand how/why that would happen.
2. Assess the probability of that to happen.

From my understanding, which might be incorrect:
I imagine that "locking" will happen to the page's UI if I, as the dev, didn't build/update the UI before the ajax call itself, which "blocks" the javascript VM, thus delaying the UI build/update. Correct or not?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes it will. While a synchronous request is outstanding, the browser waits for it to return. The probability of that happening is 100%, but if your request returns fast enough, it might not be that noticeable. Thing is, you can't count on a request returning quickly.

That's the point of ajax calls. The first a mean asynchronous, which means, "doesn't block".

Since ajax calls are asynchronous, your code can be anywhere when the request returns, which means you need a way to handle the response when the request does return. That is why you use callbacks to handle the response.

Why don't you try it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...