While more and more Web sites are being developed using AJAX technology, building a complex AJAX application is still a challenge.
What is the main cause of these difficulties? is an asynchronous communication problem with the server? Or GUI programming problems? Usually these two jobs are done by a desktop program, so why is it so hard to develop an AJAX application that implements the same functionality?
JavaScript is known to be single-threaded, but it is possible to execute a method at timed intervals via settimeout or setinterval, where Ajax can continue to execute the main logic by sending requests to the server without receiving a response. These are how to do it, the following is to explore the next.
First look at the following code:
The above code has to perform the result
As you can see from the code, Printhello and PRINTHELLO1 are executed almost simultaneously. If you have a multithreaded model, you should perform an interval of 4000ms.
The reason for this is that settimeout simply presses the event to a point in time, rather than executing it immediately. When you do this, you will also see how free your browser is.
Although JavaScript is single-threaded, browsers are multi-threaded, and typical browsers are like threads:
JavaScript engine threads
Interface Rendering Threads
Browser Event Trigger Thread
HTTP request Thread
JavaScript single-threaded is a single-threaded engine thread that handles its event queues, while browsers are event-driven, and many events are asynchronous, such as mouse click events, Settimeout,ajax callback events, and when these events occur, The browser puts these events into the execution queue and executes them when the browser is idle.
In addition, it is worth mentioning that the AJAX call is indeed asynchronous, the browser will start a new thread to send the request to the server, if the callback event is set, the callback event will be placed in the event queue based on the return status of the server.
The above is my understanding of JavaScript multithreading, the ability of personal understanding is limited, but also hope that Heroes Commission valuable advice and common learning. I hope the above introduction is helpful to everyone.