Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of JavaScript waiting for File

Shulou Source: shulou.com Published: 2022-06-01 10:31:11 09月26日 Update

This article mainly introduces the JavaScript waiting file case analysis of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this JavaScript waiting file example analysis article will have a harvest, let's take a look.

JavaScript waits for files

If you create a function to load external resources, such as scripts or files, you cannot use the content until it is fully loaded.

This is the best time to use callbacks.

This example loads a HTML file (mycar.html) and displays the HTML file in the web page after the file is fully loaded:

Wait for the file:

Function myDisplayer (some) {

Document.getElementById ("demo"). InnerHTML = some

}

Function getFile (myCallback) {

Let req = new XMLHttpRequest ()

Req.open ('GET', "mycar.html")

Req.onload = function () {

If (req.status = = 200) {

MyCallback (this.responseText)

} else {

MyCallback ("Error:" + req.status)

}

}

Req.send ()

}

GetFile (myDisplayer)

In the above example, myDisplayer is used as a callback.

The function (function name) is passed as an argument to getFile ().

This is the end of the article on "JavaScript waiting for file example analysis". Thank you for your reading! I believe you all have a certain understanding of the knowledge of "JavaScript waiting for document case analysis". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Files examples case analysis analysis content functions knowledge articles values parameters easy to operate articles timing easy to understand more look examples web pages scripts industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB macOS Redmi Shulou Technology