The smoothness of jQuery animations is not consistent

I've been working on mastering the art of scheduling animations using JavaScript, in particular with the assistance of jQuery. In an attempt to create a blinking effect for some h1 tags, I noticed that the animation was not running smoothly. It would pause intermittently before continuing.
The main animation code snippet is as follows:

function animSlideHeading() {
  $('.slide h1').animate({
    opacity: .6
  }, 500, 'swing', function() {
    $('.slide h1').animate({
      opacity: 1
    }, 500, 'swing', animSlideHeading);
  });
}

Check out this JSBin example.

Answer №1

When selecting elements with the selector $('.slide h1'), the callback function is triggered multiple times, once for each animated element. This results in animSlideHeading running repeatedly and causing issues as it progresses.

A solution to this problem is to utilize promises that resolve when all animations for the elements in the collection are completed collectively.

function animSlideHeading() {
    $('.slide h1').animate({
        opacity: 0.6
    }, 500, 'swing').promise().done(function () {
        $(this).animate({
            opacity: 1
        }, 500, 'swing').promise().done(animSlideHeading);
    });
}
animSlideHeading();

FIDDLE

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

Steps for setting up node-openalpr on a Windows 10 system

While attempting to install npm i node-openalpr, an error is occurring: When using request for node-pre-gyp https download, I encounter a node-pre-gyp warning and error message. The package.json for node-openalpr is not node-pre-gyp ready, as certain pr ...

Is it within the realm of possibility for a user to access and peruse the contents of this

I have taken measures to protect the contents of "x.txt" on my server from being viewed by any users. Here is what I have done so far: Blocked direct access to the file using .htaccess Implemented an ajax request on one of my pages to retrieve and s ...

Activate Span element when image is clicked

To show and hide different span tags when clicking on specific images, you can use the following jQuery script: $("#img1").on('click', function() { $("#div1").fadeIn(); $("#div2,#div3").fadeOut(); }); $("#img2").on('click', functio ...

Retrieving a particular div element from a different webpage using Javascript/jQuery

How can I load a specific div from another webpage? <div id="result">Loading</div> <script> $( "#result" ).load("http://bte.gep.msess.gov.pt/pesquisa_avancada.php #boxinterior" ); </script> I've tried using j ...

In certain browsers, the link changes color to white

As I assist my brother in setting up a website for his business, we encountered an issue. In some browsers, hyperlinks appear as white and therefore invisible. Interestingly, the hyperlink is white in Firefox, but not when browsing in incognito mode, see t ...

Issues with NativeScript WebView displaying HTML file

Having trouble loading a local HTML file into a webview in my NativeScript (typescript) application. Despite using the correct path, it's not loading and instead shows an error. <WebView src="~/assets/content.html" /> An error message stati ...

Tips for eliminating the empty element from a JavaScript array

Here is the code implementation I am working with: Array.prototype.abc = function(condition, t){ var arr = []; for( var i = 0; i < this.length; i++){ arr.push(condition(this[i],t)); } return arr; }; var a = [1,2,3,4]; ...

Error: The server instance pool has been terminated, requiring enhancements to the existing solution

I'm facing an issue where removing the client.close() seems to solve the problem, but I can't leave connections open to the database after the function is done. It doesn't feel secure to me. Even though the initial write completes successful ...

Images located in the "/images" folder are not being properly served as the URL is being interpreted as relative to the "/css" directory

All of the CSS files are located in the src/main/resources/public/css directory, while the images are stored in src/main/resources/public/images. When attempting to access images from style sheets, like this example: background-image: url("/images/d ...

Tips for concealing a class within JavaScript

I am looking for a way to hide a specific shipping class for products that qualify for free postmail delivery. Here is the scenario: If a product with the following link: is added to the cart and it belongs to this shipping class: shipping_method_0_adva ...

Help! I can't seem to figure out how to detect offline status in JavaScript

I am a beginner at JavaScript and I am currently working on implementing an example from the W3C website into my WebApp that will be running within the PhoneGap framework... Although it seems like a simple task, I am facing an issue where the event listen ...

Issues with Bootstrap display not functioning as expected on mobile device in live environment

My application works perfectly when accessed through a computer. When I resize the window on my computer, the Bootstrap columns are responsive and function correctly. However, after deploying to Heroku and accessing the site on my phone, the desktop layout ...

Generating a bullet list from an array of objects

Looking to create an unordered list with vanilla JS, using an array of objects to populate the list. Struggling a bit on how to accomplish this. Here is my current code: let myObj = [ {name: "Harry Potter", author: "JK Rowling"}, {name: "Hunger Gam ...

Is it safe to securely store connection credentials in javascript?

I am currently working on developing a web chat app for a Minecraft server using this API. However, the demo script I am referring to displays connection information in plain text, making it easily visible to any client's computer. Is there a way to s ...

The div is fully visible

i'm attempting to use JQuery to hide this particular div. <div id="nbar" style="background-color: #f7f7f7; box-shadow: 0 0 1px lightgray; margin-bottom: 20px; padding-left: 0; width: 100%; margin-top: 5px" class="search-navbar collapse navbar-coll ...

I am encountering an issue with selectpicker where the data from the database is automatically being displayed as selected options. I am seeking to make edits to the

I only want to see the information coming from the database, not all categories that are selected. Sorry for my English. <div class="col-xs-4"> <label>Categories</label> <!-- SEARCH JS find (selectpicker) --> <s ...

Why is the preloaded resource being reloaded?

Here is a code example I've created: <body> <link rel="preload" href="/fonts/Roboto-Regular.woff2" as="font" /> <style> @font-face { font-family: "Roboto"; src: url("/fonts/Roboto-Regular.woff2") form ...

Tailwind CSS styling appears to be malfunctioning in the production build of Next.js, despite functioning correctly in the

Despite following the proper build process with npm run build + npm run start, my Tailwind styling is not functioning correctly in the production build. Oddly enough, when I use npm run dev, the styling works as expected and my page displays with a purple ...

Column Flow in CSS Grid: Maximizing Auto-Fit - How to Optimize Row Layouts?

Having some trouble understanding how auto-fill and fit work in grid layouts. I am familiar with basic CSS, but new to working with grid layouts. I have a set of checkbox items with labels that I want to display in columns depending on screen size, with c ...

Sending Parameters from the Client-Side using Node.js

I'm struggling with a basic issue here that I can't seem to figure out. I need to pass two variables to the server side. Here's what I've tried: Client Side $("#send").click(function () { var getUsrName = $("#text").val(); var ...