Difficulty displaying a progress bar over an overlay

Issue with Progress Bar Visibility in AJAX Call: I have a requirement to display a progress bar in an overlay after the save button is clicked. However, the progress bar is only visible after we receive the response from the AJAX call and display an alert message indicating that the operation was successful. Is this the expected behavior or am I doing something wrong? Keep in mind that I need to achieve this using pure JavaScript without relying on external libraries.

<script>
  function save(){
      document.getElementById('overlaydiv').style.display = 'block';
      document.getElementById('progressBarDiv ').style.display = 'block';
      var URL = 'some url';
      ajaxFunction(URL, false);
  }
  function ajaxFunction(URL, isAsynchronousCall){
      var xmlHttp;
      var callTypeFlag = true; // is an Asynchronous Call
      if(isAsynchronousCall != null){
         callTypeFlag = isAsynchronousCall;
      }
      try{
          if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
          } else if (window.ActiveXObject) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
          }
      }catch (e){
         alert("Your browser does not support AJAX!");
         return false;
      }
      xmlHttp.open("GET", URL, callTypeFlag);
      xmlHttp.onreadystatechange = function(){responseProcessor(xmlHttp)};
      xmlHttp.send(null);
   }
   function responseProcessor(xmlHttp){
    //If the readystate is 4 then check for the request status.
      if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            alert('Settings saved successfully');
            window.close();
            opener.location.href='new URL';
         }
       }
   }
</script>

<div id="overlaydiv" style="display:none"/>  
<div id="progressBarDiv" style="display:none">
  <img border="0" src="progressBar.gif"/>
</div>
<div>
 <table>
   <tr>
     <td>
       <input type="button" onclick="save()"/>
     </td>
   </tr>
 </table>
</div>

Answer №1

Introduce a slight delay when invoking the ajaxFunction()

function save(){
      document.getElementById('overlaydiv').style.display = 'block';
      document.getElementById('progressBarDiv ').style.display = 'block';

      setTimeout(ajaxFunction, 50);//5,10,20,30,40 will work
}

Answer №2

The problem seemed to stem from my use of synchronous ajax calls. Once I switched to asynchronous, the issue disappeared.

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

Can anyone suggest a method to block the execution of Javascript commands in the console?

Regarding the inquiry mentioned in the question title. Is it possible to prevent individuals from executing functions on my webpage using the console, even though it is not necessary? I have experimented with various methods, and the code below represent ...

Designing Buttons and Titles for the Login Page

I'm currently working on developing a straightforward login page in react native and I've encountered some difficulties with styling. Does anyone have tips on how to center the text on the button? Also, is there a way to move the INSTARIDE text ...

Is it feasible to commit an object on Vue X through Actions?

I have a question regarding Vue X and actions (with commit). Can an object be passed as input in Commit? Similar to: ... action{ ResetLoginStats({commit}){ commit({ 'SetMutation1':false, 'SetMutation2':true, &a ...

Issues arise when attempting to retrieve information in NextJS from a local API

One of my coworkers has created a backend application using Spring Boot. Strangely, I can only access the API when both our computers are connected to the same hotspot. If I try to access the other computer's API and port through a browser or Postman, ...

How can I extract data from a PHP page without using a form in order to perform a query on MSSQL?

Apologies if the question title is unclear, but here's what I'm trying to accomplish: I have a list within a table with multiple items. One of the cells in the list contains checkboxes, all named in an array "itemsDone[]". Outside of this form, ...

Create a stylish navigation dropdown with MaterializeCSS

Incorporating the materializecss dropdown menu feature, I encountered an issue where only two out of four dropdown menu items were visible. Here is the HTML code snippet in question: <nav class="white blue-text"> <div class="navbar-wrapper con ...

Performing a JavaScript Axios POST request following a series of iterations using a while loop with

Just getting started with async/await and feeling a bit lost. I'm trying to figure out how to send an axios post request after a while loop finishes. Is there a way to wrap the while loop in an async function and await for it? Here's the code s ...

Unable to display items in controller with AngularJS

I am facing an issue with displaying a slider using ng-repeat in my AngularJS code. The pictures and other elements defined in the controller are not showing up on the page. Here is the JavaScript code snippet: angular.module('starter', []) .co ...

Sending javascript data to php within a modal popup

I am currently working on passing a javascript variable to php within a modal window, all while remaining on the same page (edit.php). Although I plan to tackle handling this across separate pages later on, for now, I have successfully implemented the foll ...

Server-side access to form data has been restricted

When I make a PUT request from the frontend, I am currently using the XMLHttpRequest and FormData API. However, on the server side, I am not receiving any data such as req.params, req.body, and req.query are all empty. Front-end Implementation var report ...

Utilizing Node.js with Redis for organizing data efficiently

Currently, I am in the process of configuring a Redis cache system for storing incoming JSON data in a specific format. My goal is to create an ordered list structure to accommodate the large volume of data that will be stored before eventual deletion. Th ...

The Highcharts download feature is not available when the title is removed

After setting the title of my chart to null, I noticed that I am no longer able to access the download menu on the chart. Check out this example for reference: http://jsfiddle.net/JVNjs/954/ var chart = new Highcharts.Chart({ chart: { renderT ...

Looping through alert items

Below is the code snippet I am working with: <tr ng-repeat="sce in users"> <td> <a href="/test/delete?id={{sce.id}}" onclick="return confirm('You really want to delete'+ {{sce.name}} + 'from list');" > ...

Is there a way to adjust the padding temporarily?

Important Note: Despite the misleading title of my question, it doesn't accurately reflect my actual query (sort of). Below is the code snippet in question: .one{ background-color: gray; } .two{ padding: 20px; } <div class = "one"> < ...

How can states be passed down as props in React?

This might be a beginner question, but I've been struggling with it all day. Any help would be appreciated. Apologies for the length, I just wanted to explain everything I'm having trouble with I am attempting to create custom buttons by build ...

Using Material UI with React hooks

I'm encountering an error while trying to incorporate code from Material UI. The error message is: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. Mismatc ...

Tips for maintaining ajax headers and enabling CORS in Angular resource requests

Within my ng-resource files, I have configured the ajax header as follows: var app = angular.module('custom_resource', ['ngResource']) app.config(['$httpProvider', function($httpProvider) { //enable XMLHttpRequest, indic ...

Continue running the ajax request repeatedly until it successfully retrieves results

At the moment, I am using a basic ajax call to retrieve data from our query service api. Unfortunately, this api is not very reliable and sometimes returns an empty result set. That's why I want to keep retrying the ajax call until there are results ( ...

Imitate the act of pasting content into a Textarea using JavaScript

When data is copied from the clipboard and pasted into an html textarea, there is some interesting parsing that occurs to maintain the formatting, especially with newlines. For instance, suppose I have the following code on a page that I copy (highlightin ...

What is the approach taken by jQuery in handling unsuccessful ajax requests?

I'm dealing with a code snippet that uses jQuery: $.get("go.php?action=sliderorder&right=5") .success(function(data){alert("success!");}) .fail(function(data){alert("failed!");}); After making the ajax call, I receive an error message stating: " ...