Home > Web Front-end > JS Tutorial > How to solve the problem of multiple ajax page requests and page loading blocking

How to solve the problem of multiple ajax page requests and page loading blocking

一个新手
Release: 2017-10-16 09:31:05
Original
2630 people have browsed it

I encountered multiple ajax loads in the project, and there was a problem with the loading, and it could not be loaded in the first time.

The main reason is because: the browser's rendering (UI) thread and the js thread are mutually exclusive. When executing js time-consuming operations, page rendering will be blocked. There is no problem when we execute asynchronous ajax, but when set to a synchronous request, other actions (the code behind the ajax function, and the rendering thread) will stop. Even if my DOM operation statement is the sentence before the request is initiated, this synchronization request will "quickly" block the UI thread without giving it time to execute. This is why the code fails.

#1. Rewrite synchronization into asynchronous and nest multiple ajax requests together. , call another ajax request after success, so that the second request loading will not be blocked because the first request ends first.

2. Use jquery’s Deferred object. jQuery introduced the Deferred object after version 1.5, which provides a very convenient generalization Asynchronous mechanism.

3. Use the timer setTimeout() or setinterval() function to check regularly whether ajax returns successfully.

The above is the detailed content of How to solve the problem of multiple ajax page requests and page loading blocking. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template