What causes the delay in CSS animations?

Is there a way to trigger an "updating" image to spin while a long JavaScript function is running? I am currently using JQuery to add a class called "spinning", defined in my CSS for the animation. The problem is that when I add the class and then call a lengthy JS function, there seems to be a delay in the animation starting, even though the class is applied immediately.

I need help identifying what I'm doing wrong and how to ensure the animation begins before the long-running function is executed.

You can see the issue in this JSFiddle: http://jsfiddle.net/gLas4k23/2/. The spinning starts before "doSomething" finishes, but there's a noticeable delay which is not present if the "doSomething" call is removed.

html:
<div id='updateIcon' class='update'></div>

css:

.update {
    width: 40px;
    height: 40px;
    background: url("http://s24.postimg.org/4psxulnkh/update_light.png") no-repeat;
    background-size: 40px;
    cursor: pointer;
}

.update.spinning {
    -webkit-animation: spinner 1s infinite linear;  
}

@-webkit-keyframes spinner {
  from {
    -webkit-transform: rotate(0deg);
  }
  to { 
    -webkit-transform: rotate(360deg);
  }
}

JS:
$('#updateIcon').on('click', function(e){
    console.log('i have been clicked');
    $('#updateIcon').addClass('spinning');
    doSomething();
})


function doSomething(){
 console.log('doing something')

 for(var i=0;i<20000;i++){
     console.log('waiting');
 }

}

Answer №1

The reason for the delay in your animation is due to the presence of a for loop that must finish before the animation can commence. By eliminating this loop, the animation will begin immediately.

To see the difference, check out this link: http://jsfiddle.net/gLas4k23/4/

You may need to utilize the setTimeout() function in order to achieve the desired outcome.

Answer №2

After examining the Fiddle, it seems that the issue lies within your implementation of console.log. This is causing a delay in the process. I took the liberty of updating the fiddle with my own solution. Feel free to test it out by clicking on this link: Test Now

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

Different ways to save data fetched from a fetch request

I'm fairly new to React and could use some assistance. I am trying to retrieve data from a movie database based on a search term. I have a method called getMovies where I utilize fetch to grab the data. The information is located in data.Search, but I ...

Auto-scrolling text box cursor movement

My query is quite similar to the topic discussed in this thread on automatic newline in textarea. However, my situation involves multiple textareas with a 1-row attribute, making it seem like writing on air due to the absence of visible borders (I've ...

Hover over CSS sprite

My anchor tag looks like this: <a href="#"> <i class="ico01"></i> <span>Text</span> </a> The ico01 class applies an image using CSS sprite. I want to change the background color of the entire content inside the a ...

Is it possible for me to retrieve the error message from my response?

Currently, I am working on displaying backend error messages on the frontend. My approach involves sending a response with an error code and message, then setting a state in my React component to display the error message. While I can successfully display ...

The type string[] cannot be assigned to type 'IntrinsicAttributes & string[]'

I'm attempting to pass the prop of todos just like in this codesandbox, but encountering an error: Type '{ todos: string[]; }' is not assignable to type 'IntrinsicAttributes & string[]'. Property 'todos' does not ex ...

Having trouble accessing the card number, expiration date, and CVC in vue using Stripe

Although I am aware that Stripe securely stores all information and there is no need for me to store it on my end, I have a specific requirement. When a user begins typing in the input area, I want to capture and access the card number, expiry date, and ...

Navigating using Backbone Marionette URL to Server Endpoint

I need to implement client-side validation to check if the user is logged in, and if not, redirect them to the sign-in page. The issue is that the sign-in page exists outside of my Backbone Marionette app. Is there a way in Marionette, Backbone, jQuery, or ...

Can you help me troubleshoot an issue I'm having with a basic HTTP-GET request to a Python script hosted on my

I recently came across a tutorial (Simple URL Example:Get Method section) that caught my interest. You can find the tutorial at this link: https://www.tutorialspoint.com/python/python_cgi_programming.htm After following the instructions in the tutorial, I ...

What is the method to prevent scrolling on the body while allowing scrolling on a specific div element?

<html> <body> <div class="container"> <div class="menu"></div> <div class="list"></div> </div> </body> </html> .menu{ float:left; width:50%; } .list{ float:right; width:50% ...

Do I need to include success as a parameter in my Ajax call even if my function does not return any values?

When an image is clicked, I trigger an ajax call. The image includes an href tag that opens a different page in a new tab upon click. The purpose of this ajax call is to record the user's clicks on the image for telemetry purposes in a database table. ...

When running 'npm run dev', an error occurred after the SizeLimitsPlugin emitted at 98%. The specific dependency that was

I encountered an issue while trying to incorporate Google Maps into my Laravel project and received the following error message after running npm run dev: 98% after emitting SizeLimitsPlugin ERROR Failed to compile with 1 errors 10:52:34 PM This dependen ...

The bold horizontal line in HTML tables is a bit unusual and is caused by the border-collapse property

Edit 1: The strange issue appears to be related to the border-collapse property in CSS, as it can be resolved by using border-spacing: 0px. However, the question remains, why does border-collapse result in this behavior? It seems to be connected to scaling ...

Only one div is revealed by Jquery show and hide, while the other remains hidden

I'm attempting to utilize a single href link to toggle the visibility of two other divs. The first div should be visible by default, while the second div remains hidden. <a href="#ben1" class="fa fa fa-times closer" > <p> clickab ...

Looking to pass two distinct variables using a single props with v-if in Vue-JS. Any suggestions?

I am attempting to pass different data to my other component. Essentially, when my variable firstvalue is not null, I want to send firstvalue. Currently, this setup is functioning as expected. However, I now wish to send secondvalue if it is not null. < ...

Using jQuery to Extract HTML Content from a JSON Object

I'm completely lost on what I'm doing incorrectly, but the JSON string I have looks like this: jsonp443489({"content":"<!DOCTYPE html><html><head><title>Title</title></head><body><p>Hello World< ...

Return to the beginning of the loop for another iteration

Currently, I am attempting to compare two values. If the values are equal, then the loop should break and move on to the next iteration. Below is the code snippet that I am currently using: forLoop: for (var i = 1, len = values.length; i < len; i++) { ...

verified firebase/firestore requests

I've been struggling with the process of sending authenticated firebase/firestore calls. I set up firestore in my web app using Next.js as shown below: import { initializeApp } from "firebase/app"; import { getFirestore } from 'firebase ...

Utilizing JQuery to fetch a tangible document and showcase it directly in the web browser

UPDATE: Most of the suggestions provided involve injecting the returned document into the HTML of the page, but this does not seem to be effective. It would be beneficial to understand what the ASPX code is doing: Try RenderDocument = Session("do ...

How can you avoid Ajax calls from failing due to frequent requests?

Currently, I am utilizing ajax to retrieve some data. However, I have encountered an issue where the ajax request is triggered by hovering over a button, which could potentially result in multiple requests being sent and overwhelming the server, leading to ...

Sort your Laravel pagination table effortlessly with just one click

Here is my current setup: <table class="table table-striped"> <tr> <th> Item Image </th> <th> Item Number </th> <th> Item Name </th> <th> Description </th> ...