An example of the role of JS script defer
The function of defer is to execute the script after the document is loaded, so as to avoid the problem of not finding the object. There is a good example below, and interested friends can refer to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Added defer means that it will be executed after the page is completely loaded, which is equivalent to window.onload, but is more flexible than window.onload in application!
defer is an "unsung hero" among the powerful functions of scripting programs. It tells the browser that the Script segment contains code that does not need to be executed immediately, and, used in conjunction with the SRC attribute, it can also cause these scripts to be downloaded in the background, and the content in the foreground is displayed to the user normally.
But execute the script after the document is loaded
Finally, please note two points:
1. Do not call the document.write command in a defer-type script segment, because document.write Will produce a direct output effect.
2. Moreover, do not include any global variables or functions used by the immediate execution script in the defer script segment.
A common way to optimize performance is to set the "defer" attribute in the

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the new version of the edge browser, many friends are not used to it. For example, they don't know how to disable js scripts. Today I will bring you how to disable js scripts in edge browser. Let's learn together. edge browser js script: 1. Open the browser, click the three dots in the upper right corner, and select "Settings". 2. Click "Advanced" on the left taskbar. 3. Scroll down to find "Website Permissions" and click "Manage Permissions". 4. Find “JavaScript” in “Site Permissions”. 5. Turn off the switch behind it.

What is the defer keyword in Go language? When writing programs, we often need to perform some cleanup or resource release work after a certain function or method is executed. At this time, the Go language provides a convenient mechanism. By using the defer keyword, these cleanup or resource release tasks can be postponed until the function or method returns. The defer keyword is a syntax sugar that is parsed at compile time. It defers the call of a function or method until the current function or method returns.

In the Go language, the delayed execution statement is the defer statement, and the syntax is "defer any statement". The defer statement will delay processing of the statements that follow it. When the function to which the defer belongs is about to return, the delayed statements will be executed in the reverse order of defer; that is, the statement that is deferred first will be executed last, and the statement that is defer last will be executed. statement is executed first.

Golang is a very popular programming language with very rich language features, one of which is the use of the defer keyword to complete some specific functions. In this article, we will introduce various ways to use the defer keyword. Delaying the execution of functions In Golang, the most commonly used function of the defer keyword is to delay the execution of functions. This means that after the function is executed, the function wrapped by the defer keyword will be delayed. For example, we can use the defer keyword to print the day

The defer and panic keywords are used to control exceptions and post-processing: defer: push the function onto the stack and execute it after the function returns. It is often used to release resources. Panic: Throws an exception to interrupt program execution and is used to handle serious errors that cannot continue running. The difference: defer is only executed when the function returns normally, while panic is executed under any circumstances, even if an error occurs.

Golang is an efficient, concise and easy-to-learn programming language that was originally developed by Google and first released in 2009. It is designed to improve programmer productivity and code clarity. In Golang, the function keywords defer and recover are often used together to handle errors that may occur in the program. This article will introduce the use of these two keywords and illustrate their practical application through some examples. 1. How to use defer defer is a keyword

Go language is a new programming language that has added some unique syntax and features, among which defer and panic are two very important features. This article will introduce the relationship between defer and panic in Go language as well as their usage and characteristics. Usage of defer The defer statement in Go language is used to register a function. When the execution of this function ends or the current scope ends, the registered function will be automatically executed. defer can be used

The defer function is used in the Go language to delay function calls until they are executed before the function returns, and is called in last-in-first-out (LIFO) order. Its uses include releasing resources, logging, and recovering from exceptions. The later deferred function will be called before the first deferred function.
