Ways to include a loading animation while processing calculations on a website

I'm currently in the process of building a website that involves performing calculations and plotting a graph. My goal is to have a loading animation displayed while the computations are taking place, and then have it disappear once the calculations are complete.

P.S. This is unrelated to page loading, so I don't think using a page loader is an option.

document.getElementById("submitButton").onclick=function()
{

  document.getElementById("loader").style.display='flex';  //this is the animation div 

  const permutationGeneration = (arr = []) => {
    let res = []
    const helper = (arr2) => {
      if (arr2.length==arr.length)
      return res.push(arr2)
      for(let e of arr)
      if (!arr2.includes(e))
      helper([...arr2, e])
    };
    helper([])

    delete result;
    return;
  };
    
    var randArray=Array.from({length : size}, () => Math.floor(Math.random() * 100000));
   
    timeReq = new Date().getTime();
    permutationGeneration(randArray);
    timeReq = ((new Date().getTime()) - timeReq)/1000;

}

Once the 'permutation generation' is happening, I'd like to show the loading animation. After clicking the submit button, the display changes from 'none' to 'flex'. Now, how can I change the display back to 'none' once the calculation is done and the graph is plotted?

Answer №1

If you're looking to achieve this effect, one option is to use the setTimeout function, which delays an action from happening. For instance, in the code snippet below, a loading GIF is displayed and after 2-3 seconds, the display property changes from display:none to display:block; and then back again with the GIF. The GIF appears for 2 seconds before reverting back to display:none.

// Capture form submission event
document.getElementById('loan-form').addEventListener('submit', function(e){
  // Hide results initially
  document.getElementById('results').style.display = 'none';
  
  // Show loader
  document.getElementById('loading').style.display = 'block';

  setTimeout(calculateResults, 2000);

  e.preventDefault();
});

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

Automatically install npm packages after the decision by npm to discontinue programmatic API access

Until npm 8.0, developers had the ability to programmatically install npm packages using this code snippet: const npm = require('npm'); npm.load((error) => { if (error) return console.log(error); npm.commands.install([package], (error, ...

Encountering an issue with the message: "Property 'ref' is not available on the type 'IntrinsicAttributes'."

Having trouble implementing a link in React and TypeScript that scrolls to the correct component after clicking? I'm using the useRef Hook, but encountering an error: Type '{ ref: MutableRefObject<HTMLDivElement | null>; }' is not assi ...

What is the best way for Cucumber to move on to the next annotation only after ensuring all async requests from the previous one have finished processing?

I am looking to set up a basic test using Selenium and Cucumber for logging into my web application and confirming that the main page is displayed correctly. Currently, all three tests are returning true even before the page is fully loaded. The issue ar ...

A step-by-step guide on connecting a Datepicker to an ng-model in Angular

I'm currently working on a form in HTML that has a few fields. Specifically, I am focusing on the date fields and using a datepicker for them. Below are the lines of code related to the date fields: <input tabindex="3" class="date-picker" name = " ...

A guide on transferring documents between collections using Mongoose and MongoDB

I have a list of tasks that includes both completed and incomplete items. Additionally, I have two buttons - one for deleting an item (which is functional) and the other for marking an item as done or not. I am struggling with creating a function to move ...

Can the <br> tag affect the layout in terms of horizontal space?

I am perplexed by the fact that it displays two lines vertically separated with a br tag differently than two lines vertically separated with the first line acting as a block level tag. For example, see this code snippet: https://jsfiddle.net/qzgeassf/ ...

Reduce the value of a Perl scalar variable each time an ajax script is executed

My webpage features a header with a notification button that indicates the count of unarchived notifications using this Perl code snippet: @notices_posts = System::Notice->fetch_all_posts(userid => $user->userid(), visible => 1); @regular_post ...

Tips for incorporating Javascript to restrict scrolling to just one:

My HTML page includes a section for filter_options and content. The filter_options contains a list of options. I have set up scrolling functionality where scrolling vertically changes the horizontal scroll position of the options. However, this setup is al ...

JavaScript => Compare elements in an array based on their respective dates

I have an array consisting of more than 50 objects. This array is created by concatenating two arrays together. Each object in this array contains a 'date' key with the date string formatted as: `"2017-03-31T11:30:00.000Z"` Additionally, there ...

Having trouble with JQuery toggle()? Need some assistance with fixing it?

My attempts to utilize JQuery toggle functionality have been unsuccessful. The sliding up and down animation is not smooth and instead happens very quickly. I aim to achieve a sliding effect in my code similar to this example (Please refer to Website Des ...

The element within the flexbox does not extend across the entire width, despite being designated as 100% full width

I'm currently attempting to center certain elements to ensure they are visually aligned. The upper div seems to have some odd space on the right, causing all the content within that div to be shifted and not accurately centered. On the other hand, th ...

tips for getting the sidebar to close immediately after it has been clicked

My understanding of how the menu sidebar works is a bit unclear, but here's my issue. I want the menu sidebar to close automatically when a user clicks on a menu link item or anywhere on the body. Then, it should reopen when the menu icon is pressed. ...

Navigating in a react login interface

Recently, I developed a backend code for a login system that verifies the validity of usernames and passwords. While everything is functioning as intended, I am facing an issue with automating the redirect to /dashboard. The client side operates on Port ...

"Utilizing CSS exclusively in conjunction with a PHP API for seamless integration

My CSS is working only when inline, and I'm struggling to track down the reason for this issue. As a beginner in web design, I am unsure of what steps to take to troubleshoot the problem. Since I cannot open the console to check for errors, what facto ...

An error known as "cart-confirmation" has been encountered within the Snipcart platform

I am looking to integrate Snipcart checkout in my Next.js application. However, when I try to validate a payment, I encounter an error message: An error occurred in Snipcart: 'product-crawling-failed' --- Item 1 --- [Item ID] 8 [Item Unique ID] ...

Tips for organizing intricate views with vuex

After using vuex for a few months, I'm starting to question if I am utilizing it correctly. The main issue I am facing is, How should I organize my states in a modular way? For example: I have a view where users can create and edit a resource (post ...

Pulling down the data with Ajax "GET"

When a user clicks on a button, a zip file will be downloaded. I have the necessary components in place, but I am struggling to ensure they work together seamlessly. The "GET" call will retrieve byte content with the content type specified as application/ ...

Strangely odd actions from a JavaScript timepiece

After answering a question, I encountered a problem with my JavaScript clock that displays the day, time, and date on three lines. To make sure these lines have the same width, I used the BigText plugin, which works well except for one issue. tday=new A ...

Extracting information from a recordset and displaying it in an HTML division

Currently, I am developing an Inventory Manager system to keep track of a variety of fishing equipment. Through HTML forms, I am able to input data into a SQL server database. My goal is to load data from a select statement into a specific div on my index. ...

Running a shell script on a website should always be done in a single instance

When I use html/php to call a bash script on a website, I have multiple html-buttons that initiate the same script with different arguments. The issue arises when each button click opens a new instance of the script. Is there a way to somehow resume the r ...