Slower CSS rendering as Javascript finishes its tasks

I am currently dealing with a jQuery plugin that is quite large and complex, but I will not be sharing it here to keep things simple. The issue I am facing is relatively straightforward, so I will focus on the essential code snippets:

There is a click event attached to a group of buttons:

$("ul.tick-boxes button").click(function(event) {
    event.preventDefault();
    $("ul.product-grid").addClass("loading");
    $(this).toggleClass("active");
    $theElement.trigger("filterOptionsChanged");
});

If you visit this link, you can observe these buttons in action within the left sidebar:

This CSS snippet generates a checkmark when you click on the buttons:

ul.tick-boxes button.active .tick-box::after {
    content: "\e603";
    font-family: "custom-icons";
    color: #51425d;
    top: 2px;
    left: 2px;
    font-size: 0.75rem;
    position: absolute;
}

When clicking these filter options, there is a noticeable delay in my computer's rendering process for the tick-box animation. Despite the instant toggle of the 'active' class, the visual update lags until after the product grid has been manipulated. This paradoxical behavior raises the question as to why the tick-box rendering waits for the completion of the 'filterOptionsChanged' event instead of occurring simultaneously or beforehand.

Upon closer inspection using Chrome Developer Tools, I noticed that the active classes are toggling instantly upon clicking, indicating that the issue lies within the CSS rules governing the tick-mark display.

To address this problem, I experimented by preemptively displaying the tick-marks through modifying the CSS properties even before the buttons were clicked. Despite this optimization attempt, the lag persisted.

In an effort to isolate the root cause, I temporarily disabled the 'filterOptionsChanged' event trigger. Surprisingly, this led to a significant improvement in rendering speed, suggesting that the delayed activation of the tick-boxes was somehow correlated with the execution of this particular event.

Exploring alternative solutions, I delved into utilizing jQuery Deferreds to stagger the execution of the class changes and event triggering. However, this approach failed to yield the desired outcome, leaving me puzzled about the underlying cause of this rendering delay.

Answer №1

As mentioned in a Stack Overflow post, JavaScript function calls can block the user interface until they finish executing. This also applies to the original function call that triggers a click event. One way to work around this is by using a simple solution like:

setTimeout(function() { $theElement.trigger("filterOptionsChanged"); }, 200);

This setTimeout method introduces a delay before triggering the action, giving the browser enough time to refresh the UI. Adding a loading icon during the original function and removing it within the timeout period can help enhance the visual experience for users. Additionally, exploring alternatives like web workers which simulate parallel threads might be worth considering.

Answer №2

Give this a try:

CSS:

ul.tick-boxes button.active .tick-box::after {
    content: "\e603";
    font-family: "custom-icons";
    color: #51425d;
    top: 2px;
    left: 2px;
    font-size: 0.75rem;
    position: absolute;
}

JS:

    $("ul.tick-boxes button").click(function(event) {
        event.preventDefault();
        $("ul.product-grid").addClass("loading");
        $(this).toggleClass("active").fadeIn(10, function () {
            $theElement.trigger("filterOptionsChanged");
        });
    });

Utilizing callback functions to delay the triggering. Test it out and see if it meets your needs.

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

How can we assign various class names depending on the value of an object?

I have a set of objects stored in my component. I need to dynamically apply different classes based on the value of the ErrorLevel property within each object. If an object has ErrorLevel equal to 1, I want to assign the following classes to various elemen ...

The React.js Redux reducer fails to add the item to the state

I'm just starting to learn about React.js and Redux. Currently, I am working on creating a basic shopping cart application. https://i.stack.imgur.com/BfvMY.png My goal is to have an item (such as a banana) added to the cart when clicked. (This sho ...

Revamping this snippet - JavaScript with NodeJs

Currently working on a basic CRUD application, encountering an issue with obtaining the auto-incrementing value for the latest account in MongoDB. To provide more context, I've included the snippet below to achieve the following: 1) Conduct validati ...

Easy steps to integrate React JSX using jQuery load function

I am facing a dilemma with loading files in my web app. I have a main file (master) that uses jquery .load() to call another file (loaded). While the javascript in the loaded file works perfectly, the jsx code seems to be getting ignored. Is there a way ...

A method for utilizing history.goBack in linked tabs while preventing the user from reverting to the previously selected tab

In my application, I have a settings modal that contains tabs which act as links to different paths. When the user clicks on the background outside of the modal, it triggers the history.goBack function. However, I noticed that if the user has already click ...

Swapping out LinkButtons using jQuery

[EXPLANATION] (apologies for the length) In my gridview, users have the option to add a new entry or edit an existing one. When they click on either 'Add' or 'Edit', a popup window appears for them to input the information for the new ...

Invoke a fresh constructor within a $get method in Angular's provider

I'm encountering an issue where I am attempting to utilize a function as a constructor inside the `.provider`, but I'm unable to instantiate a new constructor when it's within the `$get`. Here is my provider setup - this.$get = $get; ...

Error message: Unable to access properties of undefined object (reading 'authorization')

This is a snippet of code that handles user authentication and authorization const bcrypt = require("bcryptjs"); const jwt = require("jsonwebtoken"); const userRepository = require("../repositories/userRepository"); const SALT ...

CSS: Stack elements vertically based on their height (column-wise)

Looking for some help with designing a menu where the list items float in columns instead of rows. For example, This is how my current menu looks like: I want to change the layout to look like this: My code for the list items is as follows: .mnu-third ...

Transform PDF files into PNG format directly in your web browser

I am currently utilizing Vue.js and attempting to convert a PDF file to PNG or another image format directly within the browser. My current setup involves reading the PDF from a URL using PDF.js along with the Vue.js component pdfvuer. let instance = this ...

Ways to avoid scrolling on a fixed element

Here is the HTML setup I have... body .top .content The issue I am facing is that when scrolling reaches the end of the ul in the .top element, the background starts to scroll. This can be quite disorienting and makes the site slow on tablets. Even ...

When multiple instances are present, the functionality of dynamically generated jQuery functions ceases to operate effectively

I've developed a chat application similar to hangouts where clicking on a user generates the chat div. One feature I have is allowing users to press enter in a textarea to send text, but when multiple dynamically generated jQuery functions are present ...

Button click functionality for updating in Classic ASP is functioning correctly exclusively in Internet Explorer and not in other web browsers

Currently, I am in the process of enhancing a Classic ASP application. Within this application, there is an HTML table that is populated from a SQL database. One cell within the table contains checkboxes. Upon clicking one of these checkboxes, a popup wind ...

Is there a way to alter the background image of the logo specifically for mobile device viewing?

Here is a snippet of my HTML code: <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="{{ route('home') }}"> <img src="{{ asset('assets/images/logo-school-icon.png') }}" ...

How about a fading effect for the select box?

I'm currently working on creating a select tag that opens its options slowly with a fade in, fade out effect when the user clicks on "Actions." I've attempted to use jQuery for this feature but haven't had any luck. Here's my code: &l ...

Issue with cordova plugin network interface connectivity

I'm currently working with Ionic 2 Recently downloaded the plugin from https://github.com/salbahra/cordova-plugin-networkinterface Attempting to retrieve IP addresses without utilizing global variables or calling other functions within the function ...

Retrieve data from a JSON object within a separate function

I've encountered an issue with a JSON file. Here's a snippet of what it looks like: { "Pizza Margharita": { "id": "0", "price1": "4,50" }, "Pizza Caprese ": { "id": "1", "price1": "4,00 ...

I am experiencing issues with the functionality of the Search Button in my code

I am experiencing an issue with the Search Button in my Search Form. I created the Search form using only html and pure CSS to test whether JS is necessary for a Search form with Drop Down Filter! Do I need to incorporate some JS code or is it feasible to ...

Is there a way to change the color of a number's font based on whether it is preceded by a "+" or "-" sign?

I am currently working on a stocks widget and I have a specific requirement. Whenever there is a change in value in the Change or Changeinpercent columns, I need to dynamically set the font color to red for a decrease (-) or green for an increase (+). Her ...

What is the most efficient way to merge two form fields and send them as a single field using jQuery?

My current issue is that I have created a form where users can input multiple items with quantities, and they can add more fields if necessary. You can access the form at: In order for my shopping cart to recognize multiple items submitted, it needs to b ...