What is the best way to make the cursor display as 'wait' while a script is running?

During the execution of a lengthy javascript function, I would like to change the cursor style to 'wait' and then revert it back to normal once the function is complete.

I initially tried using the following code:

document.body.style.cursor = 'wait';
// long scripting here
document.body.style.cursor = 'auto';

However, when hovering over elements such as <input>, the cursor changes to its default type instead of staying as 'wait'. (Verified through individual console commands).

In my research, I came across a related question that suggests creating a CSS rule:

html.wait, html.wait * { cursor: wait !important; }

and applying it using this js:

document.querySelector("html").classList.add("wait");
// long scripting here
document.querySelector("html").classList.remove("wait");

Unfortunately, this method does not seem to take effect while the script is running.

Answer №1

To ensure smooth operation, introduce a short delay in JavaScript to allow the browser to handle repainting processes.

const DELAY_FOR_REFRESH = 25; // milliseconds
document.querySelector("html").classList.add("wait");
setTimeout( () => {
  // lengthy scripting here
  document.querySelector("html").classList.remove("wait");
}, DELAY_FOR_REFRESH);

Based on initial testing, it's recommended that the DELAY_FOR_REFRESH value should be set to at least 20ms to prevent the browser from skipping the repaint/update process before executing long scripts.

Note: The cursor reset back to 'auto' should be included within the setTimeout() function (not after it).


A potential sequence of events in this scenario may unfold as follows:

  1. DOM is updated to display the 'wait' cursor
  2. Browser does not immediately reflect changes due to CPU constraints
  3. JavaScript schedules tasks for later execution
  4. CPU remains idle until scheduled task kicks in
  5. Browser utilizes idle CPU resources to perform necessary repaint operations
  6. Scheduled task begins processing
  7. Task completes, including resetting the cursor

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

Obtaining the value of a specific dynamic `<td>` element using jQuery

My page layout resembles this design (I am unable to post an image due to insufficient points, so I have it hosted elsewhere) My goal is for jQuery to identify the specific email when a user clicks on the delete button. For instance, if a user clicks on & ...

What steps should be taken to fix the sinopia installation error?

While operating on redhat5.9, I encountered an exception related to 'make'. I am curious about what they are trying to create. It seems like it's related to JavaScript. [root@xxxx bin]# npm install -g sinopia --python=/usr/local/clo/ven/pyt ...

What is the best way to fix multiple dropdown menus opening simultaneously in Vue.js?

I am working on an application that generates todo lists for tasks. These lists can be edited and deleted. I am trying to implement a dropdown menu in each list to provide options for updating or deleting the list individually. However, when I click on the ...

flickering effect problem

What could be causing the issue where [@fadeInOut] only works on the initial page load when toggling isExpanded to hide or show content? Due to [@fadeInOut], the content stops showing up. Any thoughts on what might be causing this and any alternative solut ...

The MUI makeStyles() class has been implemented, yet the styles are not being displayed when using classList.add('class-name')

Currently, I am utilizing MUI makeStyles() to create a series of CSS classes. Within these classes, I am dynamically including and excluding one specific class to my Box element during file drag-and-drop events. The class is successfully added, as I can o ...

Ways to layer two divs on each other and ensure button functionality is maintained

In the process of developing a ReactJS project, I encountered the challenge of overlapping my search bar autocomplete data with the result div located directly below it. For a more detailed understanding, please take a look at the provided image. Here&apo ...

Tips for changing the TextField variant when it receives input focus and keeping the focus using Material-UI

When a user focuses on the input, I'd like to change the variant of the TextField. The code snippet below accomplishes this, but the input loses focus. This means the user has to click again on the input to focus and start typing. import React, { useS ...

Tips for parsing a JSON object from a servlet to a JSP page

As a newbie in the world of JSON and AJAX, I find myself struggling to create a chart using chart.js. I have a servlet that reads SQL statements and a JSP that processes these requests, but I lack experience in this type of programming. If anyone out there ...

Tips for creating animations using parent and child components in Angular

Despite my best efforts, it seems like this should be functioning properly... but unfortunately it's not... I'm attempting to achieve a transition effect on the parent element (ui-switch-groove) while the child element (ui-switch-dongle) moves. ...

What is the process for transforming a JSON object format to another in node.js?

I found myself with a json object structured as follows: { "sample1": { "4": 2245, "5": 2175, "6": 3495, "7": 1845, ... "11.5": 1674, "12.5" ...

Issue with Vue.js - GET response not being stored in this.data

<!DOCTYPE html> <html> <head> <title>Welcome to Vue</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> </head> <body> <div id="app"> <button v-on:click="sendTime">Lo ...

Looking to pass multiple props and run a function for each one? Here's how!

There is a scenario where I have two separate times in minutes format that need to be converted to 24-hour format without separators. I am currently using a function for this conversion and then using momentjs to transform it into the required format. Whil ...

The React.js project I created is showcased on GitHub pages and has a sleek black design

After developing a project using React.js and deploying it on github pages, everything was functioning smoothly. However, I encountered an issue where the screen turned black after logging in and out multiple times. Are there any suggestions on how to reso ...

Disabling form submission when pressing the enter key

Is there a way to prevent a submit action from occurring when the enter key is pressed within an ASP:TextBox element that triggers an asyncpostback upon text change? Instead, I would like it to click on another button. The Javascript function I am using wo ...

Issue with react-intl not updating the placeholder value

Is there an error in my implementation? I have utilized the intl.formatMessage() API (https://github.com/yahoo/react-intl/wiki/API#formatmessage) in this manner: this.intl.formatMessage({id:'downloadRequest.success',values:{correlID:'test& ...

Having difficulty positioning the image on the left side

Learning CSS can be a challenge, but I recently made progress by using display flex to create a navigation bar. However, I encountered an issue where the background image for the navbar doesn't align with the left side of the screen and results in unw ...

Is the Bootstrap popover triggering the same action/event twice?

Have you ever encountered the issue of Bootstrap's popover repeating an action twice? It can be frustrating, especially when trying to submit a form inside the popover's data-content using AJAX. The form data gets repeated twice and the form is p ...

Analyzing the elements of an array in JavaScript and making modifications to it based on their values

Looking for a Solution Current Data- id price 1001 200 1001 150 1002 300 1003 50 1002 70 1004 30 Desired Outcome id price 1001 350 1002 370 1003 ...

Methods for attaching values to individual list items in Vue to switch images

Is there a way to toggle between two images by triggering a function on click event for different list items? Currently, all the list items display the same image because of a global value. How can I modify the code to toggle the images individually for ...

`At a loss: jQuery failing to retrieve JSON data`

I'm having trouble with a basic script that is supposed to fetch data from a JSON feed and display it in an alert. Despite having loaded jQuery correctly and checked for common issues, the code doesn't seem to be working. I am using jQuery and ca ...