`There is a delay in rendering the background image on Chrome`

Once I apply a class to my button element using JavaScript, the background image (.gif) that is supposed to display afterwards takes an unusually long time to render.

The button serves as a form submission. Upon being clicked, a class of "clicked" is dynamically added to it through JavaScript. The CSS style dictates that when the button has the class "clicked," it should show a loading animation as its background image. However, this animation often appears delayed (about 0.5-1.0s after clicking), sometimes not even appearing before the page reloads following a submit. Even when pausing the JavaScript execution after adding the class and inspecting the DOM using developer tools, the class is already present but the image fails to display in most cases.

To replicate this issue, here's a fiddle demonstrating the behavior: http://codepen.io/rjk/pen/rGntD

This problem seems to be specific to the Chrome browser, as Firefox shows everything instantly as expected.

If anyone has insight into how to prompt Chrome to immediately reflect the change and eliminate this unpredictability, your input would be greatly appreciated.


A simplified version of the JavaScript code causing this issue is as follows:

function loadingClickHandle(button, style) {
  jQuery(button).addClass(style + "-clicked");
}

loadingClickHandle(this, "button-blue");
$("form:first").submit();

Answer №1

If you're looking to optimize load times by converting images to base64 strings, here's a solution that might help. By encoding the image data directly into the HTML, you can eliminate the need for extra server requests and reduce latency.

It's worth noting, however, that while this method works well for smaller images, it can actually increase the size of the data due to the overhead of base64 encoding.

According to estimates, Base64-encoded binary data is approximately 1.37 times larger than the original data size, plus an additional 814 bytes for headers.

For more information about base64 encoding, check out this link.

The snippet provided below demonstrates how you can apply this technique in your code:

.blue-button {
  background-color: #1b9bdd;
  color: white;
  height: 30px;
  padding-left: 14px;
  padding-right: 14px;
}

.blue-button.clicked {
  background-image: url(data:image/gif;base64,R0lGODlhEAAQAPcX...); /*truncated for brevity*/
  background-repeat: no-repeat;
  background-position: center;
  color: transparent;
}

You can view a live demo of this implementation on CodePen.

To convert your images to base64 strings, consider using a tool like this one.

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

Identifying a failed Ajax Request in JavaScript

How can I check if an Ajax request failed to load a file? Here is the code I'm currently using: var pro = undefined; var xmlhttp; if (window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTT ...

How can I position a Button at the center of my Grid item using Material UI?

In my React 16.13.0 application, I am utilizing Material UI's Grid and trying to center the content of a button in the middle of a row. Here is what I have tried: center: { alignItems: "center", width: "100%", }, halfRow: { width: "5 ...

Switch out the name of multiple elements with mootools

Is there a Moo tool that can replace multiple element IDs? I currently have the following code: $$('myelement').each(function(el){ var get_all_labels = el.getElements('label'); var get_label_id = get_all_l ...

What could be causing Typed.js to not function properly in my situation?

Having an issue with typed.js not functioning properly. Here is the code snippet: <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="js/jquery-1.11.3.min.js"></script> <!-- Include all compiled plugins ...

A code to ensure input values are always set to the maximum possible value

I recently developed a script that monitors the live value property of 3 <input> elements, correcting them if the live value is outside the specified range. However, I encountered an issue where the script automatically sets the value to the max as s ...

Struggling to get the Hover feature on Link from Material-UI to function properly

I've been attempting to hover over Link from Material UI, but unfortunately, it's not working as expected. The CSS styles are not being applied for some unknown reason, and I can't seem to figure out why. Additionally, the Button class is al ...

The drop-down list unexpectedly closes at the most inconvenient moment

I am looking to create a search input with a drop-down list. The requirement is for the list to close when the focus or click is anywhere except the search input. I have added a function listClose() to the "blur" listener, but now I am unable to capture t ...

Aligning text in the middle of an image both vertically and horizontally

I've been experimenting with different methods to vertically align caption text in the middle of an image, but I haven't had much luck. I've tried using table-cells and other techniques, without success! Currently, I have an <li> elem ...

Issue with Vue-Multiselect: Unselecting a group of pre-loaded values is not functioning as expected

My code: https://jsfiddle.net/bgarrison25/tndsmkq1/4/ Html: <div id="app"> <label class="typo__label">Groups</label> <multiselect v-model="value" :options="options" :multiple="true" group-values="libs" g ...

There was a serious issue: The mark-compacts were not working effectively near the heap limit, resulting in allocation failure - the JavaScript heap ran out of memory during the

I recently set up a t2.micro server on AWS and encountered an issue when running our application with the command "sudo npm start". The error message I received was: "FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript he ...

Using AngularJS to send a model to ui-select

Here is the scenario at hand: A form includes a directive known as <timeZone ng-model="formModel.timeZone" This directive communicates with a web service to fetch timezone information The directive then presents the timezones in a ui-select dropdown ...

"Exploring the world of custom middleware in NextJs on a localhost

Question regarding nextjs page middleware: the documentation states that the request object should include geo, IP, ua, cookies, and nexturl properties. Visit the link for more information. I am attempting to retrieve the user's location using page m ...

Utilizing jQuery to Extract Values from a List of Options Separated by Commas

While usually simple, on Mondays it becomes incredibly challenging. ^^ I have some HTML code that is fixed and cannot be changed, like so: <a class="boxed" href="#foo" rel="type: 'box', image: '/media/images/theimage.jpg', param3: ...

Tips on how to optimize form input mapping without losing focus on updates

I am facing an issue with a form that dynamically renders as a list of inputs. The code snippet looks like this: arrState.map((obj,index)=>{ return( <input type="text" value={obj} onChange{(e) => stateHandler(e,index)}<inpu ...

Ensuring that hover remains active despite mousedown in JavaScript, CSS, and HTML

It appears that in browser-javascript hover events are deactivated when the mouse button is pressed down, as demonstrated by the following example: (Please run the code snippet to witness what I am referring to.) * { user-select: none; } #click, #drag, ...

Tips for centering or aligning a component to the right using Material UI?

Is there an efficient method to align my button to the right of its parent in Material UI? One approach could be using: <Grid container justify="flex-end"> However, this would also require implementing another <Grid item />, which m ...

What is the process for creating the token?

I've encountered an issue when generating a token while creating a user account. Despite my efforts, I keep getting an empty set. What could be causing this problem? Could there be an error in the syntax? Take a look at the controller file: import m ...

Excessive Empty Area beneath <td>

I'm currently working on creating an e-book and I have encountered a concerning issue. The <figure> and <figcaption> elements are appearing as unknown tags in Sigil. To work around this problem, I decided to use a <table> for display ...

The range slider initiates with the first alphabet

I have created a range slider with values from 0 to 4, displaying as 0, 1, 2, 3, 4. Currently, the minimum value is set as 0 and the maximum value is set as 4. Now, I am looking to replace 0 with 'F'. For example: F, 1, 2, 3, 4. Is it possible t ...

When the forward button is pressed multiple times in the carousel, the alignment changes

I recently noticed that the alignment of my carousel gets disturbed when I repeatedly click the forward button (>). How can I ensure that the carousel moves smoothly one item at a time when I click the forward or backward buttons? <div class="contai ...