Display issue with loading animation

After clicking the button and seeing the alert message, I noticed that the loading animation does not display during the

await new Promise(r => setTimeout(r, 2000));
.

/* script.js */

async function refreshStatus() {
  document.getElementById("loading-overlay").display = "flex";
  document.getElementById("loader").display = "block";
  alert("trying");

  try {
    await new Promise(r => setTimeout(r, 2000));
    var response = await fetch(
      the_url, {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*',
        },
        body: JSON.stringify({"ok": "yes"})
      }
    );
  }
  catch (_error) {
    document.getElementById("error-div").display = "flex";
    document.getElementById("error-div").innerHTML = _error.message;
    return;
  }
}
/* loading.css */

#loading-overlay {
  z-index: 10;
  display: none;
}
.loader {
  border: 16px solid #f3f3f3;
  border-top: 16px solid #3498db;
  border-radius: 50%;
  width: 120px;
  height: 120px;
  animation: spin 2s linear infinite;
}
#error-div {
  z-index: 20;
  display: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <!-- <link href="loading.css" rel="stylesheet" /> -->

  <!-- <script src="script.js" type="text/javascript" /> -->
</head>
    
<body>
  <div id="loading-overlay">
    <div id="loader"></div>
    <p id="error-div"></p>
  </div>
  <button type="button" id="button_queue_go" onclick="refreshStatus();">Show Status</button>
</body>
</html>

Answer №1

There were a couple of issues in your code that caused the 'loading' circle not to display correctly:

  • In your HTML code, you should use .style.display instead of just .display to change the display property of an HTML div element via JavaScript.
  • In your loading.css file, you needed to target the loader using #loader instead of .loader since it is an id in the HTML markup.

#loading-overlay {
  z-index: 10;
  display: none;
}
#loader {
  border: 16px solid #f3f3f3;
  border-top: 16px solid #3498db;
  border-radius: 50%;
  width: 120px;
  height: 120px;
  animation: spin 2s linear infinite;
}
#error-div {
  z-index: 20;
  display: none;
}
@keyframes spin {
    from {
        transform:rotate(0deg);
    }
    to {
        transform:rotate(360deg);
    }
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <link href="loading.css" rel="stylesheet"/>

        <script type="text/javascript">
            async function refreshStatus()
            {
                document.getElementById("loading-overlay").style.display = "flex";
                document.getElementById("loader").style.display = "block";
                alert("trying");
                try
                {
                    await new Promise(r => setTimeout(r, 2000));
                    var response = await fetch(
                        the_url,
                        {
                            method: 'POST',
                            headers: {
                                        'Accept': 'application/json',
                                        'Content-Type': 'application/json',
                                        'Access-Control-Allow-Origin': '*',
                                    },
                            body: JSON.stringify({"ok": "yes"})
                        }
                    );
                }
                catch (_error)
                {
                    document.getElementById("error-div").style.display = "flex";
                    document.getElementById("error-div").innerHTML = _error.message;
                    return;
                }
            }
        </script>

    </head>
    <body>
    <div id="loading-overlay">
        <div id="loader"></div>
        <p id="error-div"></p>
    </div>
    <button type="button" id="button_queue_go" onclick="refreshStatus();">Show Status</button>
    </body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Ant Design form is not updating values after using setFieldsValue in Testing Library

Currently, I am working with the testing-library/react-hooks library and using renderHook. One issue I'm facing is that I am unable to set a value for my antd form using setFieldsValue. It seems like the value is not being set properly. What could be ...

"Compilation error: 'not defined' is not recognized, 'no-undef

I am currently working on a login form that will fetch values from this API: However, the password field is currently empty, allowing any password to be accepted. This results in the error: Failed to compile 'userName' is not defined no-undef; ...

Refresh the data in an existing Highstock chart with JavaScript only

I'm currently updating a website and unfortunately, I do not have access to the original code. All I am able to do is append new code at the end of it. The existing code consists of a highstock chart with specific data attributes. Here is the snippet ...

Maintain scrolling at the bottom with React.js

Is there a way to make a div element increase in height through an animation without extending beyond the viewable area, causing the window to automatically scroll down as the div expands? I am looking for a solution that will keep the scroll position lock ...

Maximizing the efficiency of table search with AngularJS filter

Currently, I am implementing pagination in my tables using a library called Angular Table. This library utilizes the ng-repeat directive to create its own array from the data I provide. Due to this implementation, I am unable to utilize the search filter ...

javascript: update hidden field if date is past January 15th

Having a bit of trouble with this one after a full day! The form contains a hidden field called 'grant_cycle'. If the form is submitted after January 15th, it should have the value 'Spring, [year]', or if after July 15th, it should be ...

Trouble with Bootstrap 5 Dropdown Functionality

Currently, I am using Bootstrap 5 in conjunction with Django to create a website and I'm encountering difficulties with getting a dropdown feature to work properly. Despite copying the code directly from w3schools, it fails to function when I load the ...

Tips for retrieving information from a highstock chart

Imagine I have a sample highstock chart on my website, similar to the one at this link. Is there a way to extract the data from the chart itself, even if the data used for creating the chart is not accessible to others? <img src="http://www.highchart ...

Encountering difficulties triggering the click event in a JavaScript file

Here is the example of HTML code: <input type="button" id="abc" name="TechSupport_PartsOrder" value="Open Editor" /> This is the jQuery Code: $('#abc').click(function () { alert('x'); }); But when I move this jQuery code to a ...

I am looking to implement a permanent change using PHP Ajax

I am facing an issue with the "Add as Buddy" button on my webpage. I want it to change permanently to "Pending Request" once clicked, but it keeps reverting back to "Add as Buddy" whenever I refresh the page. Can anyone suggest a solution for this problem? ...

How come it's not possible to modify the text of this button right when the function kicks off?

When I click a button, it triggers a JavaScript function. The first line of code within the function uses jQuery to change the HTML of the button. However, the button's text does not update in the browser until after the entire function has completed, ...

What steps can be taken to default to a Google web font when Helvetica is not available on the user's device?

Is there a way to have a font fall back to a specific one on Google Web Fonts (Open Sans) if the user does not have Helvetica installed? I'm looking for an efficient method where downloading the font is only necessary if needed. For example, I don&ap ...

How to send a JavaScript variable to Flask and trigger an AJAX reload action

Introduction: I have explored similar inquiries and attempted to apply relevant code/concepts but without success. -https://stackoverflow.com/questions/43645790/passing-javascript-variable-to-python-flask -https://stackoverflow.com/questions/10313001/is- ...

What is the method for creating a clickable link using only HTML5 and CSS3 on a div?

Throughout the ages, this question has persisted, with answers that have stood the test of time. I've explored various solutions, yet none have met my expectations. I aim to create a fully clickable link for div .content1 without relying on text or ...

Using ng-mouseover along with ng-if and an animated class causes issues

Whenever I hover over a link, it triggers a change in a variable value which then displays a <p> tag using ng-if. The code snippet looks like this: <div class="position text-center" ng-mouseover="social=1" ng-mouseleave="social=-1"> &l ...

Is there a way to utilize javascript std input/output in repl.it effectively?

I created a straightforward program that calculates the factorial of a specified number, and I am interested in running it on repl.it. During its execution, I would like to interact with standard input and output through the command line. Is there a way ...

Node.js variable initialization

Here's the issue I'm facing: in this function, I'm attempting to return an array of objects. When I use console.log within the forEach loop, the array is populated as expected. However, just before my return statement, it appears empty. http ...

Loading the children of a specified div ID using AJAX

Imagine having the following code: $('a').each(function() { $(this).click(function(e) { e.preventDefault(); var href = $(this).attr('href'); $('#somediv').load(href + ' #foo'); }); }) ...

The radio button's checked attribute fails to function

I am currently working on adding a hover effect to radio button labels, and while it is functioning correctly, I would like to achieve the following: When a radio button is selected, I want the corresponding label to have a background color. Despite tryi ...

XSL is the key component in creating a unique Custom Slider that remains stationary

I am currently working on a jQuery slider project and I am encountering an issue with getting the images to slide properly. The arrows seem to be functioning as expected, indicating that the necessary classes have been added, but the actual sliding action ...