You can display a preloader element while waiting for a server response using fetch
by following these steps:
Create a variable that references the element to be displayed as the preloader.
const preloader = document.querySelector('.preloader');
Before making the fetch
request, display the preloader element.
preloader.style.display = 'block';
Make the fetch
request as you would normally.
fetch('/api/data')
.then(response => response.json())
.then(data => {
// handle the data returned
})
.catch(error => {
// handle any errors
});
After the request is complete, hide the preloader element.
preloader.style.display = 'none';
Here's the complete example:
const preloader = document.querySelector('.preloader');
preloader.style.display = 'block';
fetch('/api/data')
.then(response => response.json())
.then(data => {
// handle the data returned
preloader.style.display = 'none';
})
.catch(error => {
// handle any errors
preloader.style.display = 'none';
});
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-22 12:52:59 +0000
Seen: 17 times
Last updated: Jun 22 '23
Despite setting `verbose` to `False`, why does Moviepy still display a progress bar?
How can I display a loading message in aggrid using react js?
How can Xcode 13.1 display the source code framework of an app?
How can I display the address field from the lead object in various sections?
Can AG-GRID display everything in a single column?
How can I modify Angular 8 to display a blank page without any error messages?
How can you display a Pandas Dataframe using a for loop?
How can the output of VBA be displayed in a cell within Excel?