Home Web Front-end JS Tutorial Understand the setTimeout_javascript technique with a delay time of 0

Understand the setTimeout_javascript technique with a delay time of 0

May 16, 2016 pm 07:04 PM
settimeout Delay

Let’s take a look at my previous article: 9 Pitfalls and Comments on JavaScript, the issues mentioned in point 9 Focus Pocus. The original author has a biased understanding of this. In fact, it is not just a problem with IE, but a problem with the existing JavaScript engine's thread implementation (I don't have much concept about threads. If it is wrong, I hope readers can give me some advice). . Let’s look at 1 and 2. If you can look at the source code, you will find that our task is very simple, which is to add an input text box to the document, focus and select it. Please click on each one now. You can see that 1 cannot be focused and selected, but 2 can. The difference between them is that when executing:

input.focus();
input.select();
, 2 has an additional setTimeout peripheral function with a delay time of 0, That is:

setTimeout(function(){
input.focus();
input.select();
}, 0);
Follow JavaScript: The Definitive Guide 5th 14.1 says:

In practice, setTimeout will tell the browser to enable the function registered within setTimeout after it has completed the execution of any event handlers for currently deferred events and updated the current state of the document.

Actually, this is a technique to jump out of the queue of tasks that need to be executed. Back to the previous example, when the JavaScript engine executes onkeypress, since there is no synchronized execution of multiple threads, it is impossible to process the focus and select events of the newly created element at the same time. Since these two events are not in the queue, after completing onkeypress, The JavaScript engine has discarded these two events, as you can see in Example 1. In Example 2, because setTimeout can jump tasks from a certain queue into a new queue, the expected results can be obtained.

This is the real purpose of setTimeout with a delayed event of 0. Here, you can take a look at Example 3. Its task is to update the entered text in real time. Please try it now. You will find that the preview area is always one beat behind. For example, when you enter a, a does not appear in the preview area. When b is entered, a appears calmly. In fact, we have a way to synchronize the preview area with the input box. I have not given an answer here, because the above is the solution, try it yourself!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to turn off the delay in Douyu live broadcast? -How to watch the replay of Douyu live broadcast? How to turn off the delay in Douyu live broadcast? -How to watch the replay of Douyu live broadcast? Mar 18, 2024 am 10:55 AM

How to turn off the delay in Douyu live broadcast? 1. The user first clicks to enter Douyu Live, as shown in the picture. 2. Then the user clicks "Settings" in the "Douyu Live" window, as shown in the figure. 3. Then in the "Settings" window, click "Advanced", as shown in the figure. 4. Finally, in the "Advanced" window, the user can cancel the delay by turning off "Low latency mode is on by default", as shown in the figure. How to watch replays of Douyu live broadcast? 1. In the first step, we first find the Douyu live broadcast software icon on the computer desktop, then right-click and select the "Open" option. 2. In the second step, after opening the Douyu live broadcast software, we find "Follow" on the left side of the page. option, click to open this option and find a host you like on the right page, click the "Recording" option 3. The third step, proceed

Application of queue technology in message delay and message retry in PHP and MySQL Application of queue technology in message delay and message retry in PHP and MySQL Oct 15, 2023 pm 02:26 PM

Application summary of queue technology in message delay and message retry in PHP and MySQL: With the continuous development of web applications, the demand for high concurrency processing and system reliability is getting higher and higher. As a solution, queue technology is widely used in PHP and MySQL to implement message delay and message retry functions. This article will introduce the application of queue technology in PHP and MySQL, including the basic principles of queues, methods of using queues to implement message delay, and methods of using queues to implement message retries, and give

What is the difference between settimeout and setinterval What is the difference between settimeout and setinterval Aug 15, 2023 pm 02:06 PM

The difference between settimeout and setInterval: 1. Trigger time, settimeout is one-time, it executes the function once after setting the delay time, while setinterval is repetitive, it will execute the function repeatedly at the set time interval; 2. Execution times, settimeout is only executed once, and setinterval will be executed repeatedly until canceled.

How to solve network latency issues in Java How to solve network latency issues in Java Oct 08, 2023 am 11:21 AM

How to solve the network delay problem in Java Network delay refers to the time delay between the sending and receiving of data due to various reasons during the data transmission process. When conducting network communications or developing network applications, we often encounter network latency problems. This article will introduce some methods to solve network latency problems in Java and provide specific code examples. 1. Using multi-threading Network delays are usually caused by blocking of network requests. In order to avoid network requests blocking the main thread, we can use multi-threading to handle the network

How to solve the delay problem when dragging windows with the mouse in Windows 10 How to solve the delay problem when dragging windows with the mouse in Windows 10 Jan 06, 2024 pm 05:28 PM

In the process of using the win10 system, if you encounter delays and freezes when dragging the window with the mouse, the editor thinks it should be a problem with the system settings or a driver problem. You can try to reinstall the driver or use the main.cpl code during operation to solve the problem. Let's take a look at the detailed solution to the delay in dragging the window with the mouse in Win10. What to do if there is a delay when dragging the window with the mouse in Win10: Method 1 (common to wired and wireless): 1. On the Win10 system desktop, press the "win+r" shortcut key to open the run window, enter: main.cpl and press Enter to confirm. 2. Then open the mouse properties dialog box, click the mouse button, and adjust the mouse double-click speed by sliding the cursor in "Double-click speed". 3. Then click

The medicine cures the disease: One trick to win, completely solve the problem of PHP server optimization, and say goodbye to lags! The medicine cures the disease: One trick to win, completely solve the problem of PHP server optimization, and say goodbye to lags! Feb 20, 2024 am 09:43 AM

PHP is a popular WEB development language, but it often encounters stuck and delay problems. This not only affects the user experience, but also causes headaches for developers and operation and maintenance personnel. In order to solve this problem, multiple teams and institutions have proposed various solutions, but one of them stands out and has won many praises for its efficiency and reliability. Identify the root problem To solve the problem of PHP lagging and delay, the first step must be to identify the root problem. The most common reasons are: System configuration: Lack of sufficient system resources, including CPU, RAM and disk space. Software environment: Improper configuration of PHP-related software, including PHP itself, web servers and related plug-ins. Network issues: Lack of a solid network or relationship between the PHP server and other systems

Super human login is delayed, please try again. Solution Super human login is delayed, please try again. Solution Mar 30, 2024 pm 08:31 PM

In the Super Human game, some players experienced login delays. Please try again. Clicking the confirmation button will prompt you to exit the GeeGee client immediately. Today I have brought you a solution, come and take a look. Super human login is delayed, please try again. The solution "Steammustberunningtoplaythisgame" probably means that steam is not allowed to run this game. It may be because steam is not installed, it may be because it is not run as an administrator, or it may be stuck. Solution: 1. The first method is to download a steam platform, open the installation disk, and put the contents of the crack folder into the game folder. 2

Why is the large model of China's most powerful AI research institute late? Why is the large model of China's most powerful AI research institute late? Jun 26, 2023 pm 07:43 PM

Produced | Author of Huxiu Technology Group | Editor Qi Jian | Chen Yifan's header image | FlagStudio "Will OpenAI open source large models again?" When Zhang Hongjiang, Chairman of the Zhiyuan Research Institute, asked the CEO of OpenAI to attend the 2023 Zhiyuan Conference online When SamAltman asked about open source issues, SamAltman smiled and said that OpenAI will open more codes in the future, but there is no specific open source timetable. This discussion comes from one of the topics of this Zhiyuan conference-open source large model. On June 9, the 2023 Zhiyuan Conference was held in Beijing, and all seats were packed. At the conference site, AI-related words such as "computing power", "large model" and "ecology" appeared from time to time in the chats of participants, as well as various companies in this industry chain.

See all articles