The delay() method in jQuery does not function properly when used in conjunction with the addClass() function

After applying the delay() function before the addClass() function, I noticed that the class circle1 does not pause for the specified time.

Below is the code snippet I am referring to:

CSS:

.but{display:block; width:40px; height:40px; background-color:#F00; position:absolute;}

.circle1{display:block; width:145px; height:145px; position:absolute; left:10px; bottom:0px; border-radius:50%; border:5px solid rgba(100%,0%,20%,0.7); background-image:url(New%20folder/1st.jpg); background-position:center; background-repeat:no-repeat; background-size:cover; transform:scale(0);}
.zoom{transform:scale(1); transition:All 0.4s ease;}

JQuery:

$(document).ready(function() {
    $(".but").click(function() {
         $('.but').fadeOut(300);
         $('.circle1').delay(1000);
         $('.circle1').addClass("zoom");
    });
});

Answer №1

If you want to create a delay in your JavaScript code, it is recommended to use the setTimeout function instead of using .delay(), which is meant for working with queues and may not work as expected with certain methods like addClass().

$(".button").click(function() {
     $('.button').fadeOut(300);
     setTimeout(function(){
         $('.box').addClass("expand");
     }, 1000);        
});

Alternatively, you can utilize the .queue method:

$(".button").click(function() {
     $('.button').fadeOut(300);    

     $('.box').delay(100)
               .queue(function() {
                   $(this).addClass("expand");
                   $(this).dequeue();
               });

});

Answer №2

Approach 1 setTimeout()

$(".button").click(function() {
         $('.button').hide(300);
         setTimeout(function(){$('.shape').addClass("enlarge")},1000);
    });

Approach 2 .queue

$(".button").click(function() {
    $('.button').hide(300);
    $('.shape').delay(1000).queue(function () {
        $('.shape').addClass("enlarge");
    });
});

Answer №3

.delay() is designed to work with jQuery methods that utilize the animation queue on the same DOM element. Since .addClass() does not interact with the animation queue, it is not compatible with .delay().

To make addClass() work in a queued manner, you can call it within a function that is part of the animation queue, or simply use setTimeout().

One way to achieve this is by following this approach:

$(document).ready(function() {
    $(".button").click(function() {
         $('.button').fadeOut(300);
         setTimeout(function() {
             $('.shape1').addClass("enlarge");
         }, 1000);
    });
});

Answer №4

When using the methods in jQuery like .delay(), it works by utilizing a queue system to delay the execution of the next method in line. The default queue is called the fx queue. However, the addClass method does not operate within a queue which can cause it to not work as expected.

To remedy this, you can add a function that adds the desired class to the target element to the queue by utilizing the queue() method as shown below:

$(document).ready(function() {
  $(".but").click(function() {
    $('.but').fadeOut(300);
    $('.circle1').delay(1000).queue(function() {
      $('.circle1').addClass("zoom");
    });
  });
});
.zoom {
  color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="but">but</button>
<div class="circle1">div</div>

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

Issues with basic form functionality in Jquery ajax

I am attempting to send form data to a PHP file via AJAX using jQuery. I expected it to be straightforward since it's just one text box. When the submit button is clicked, the form data should be serialized and sent to submit.php. After that, I should ...

Maximize background width with a gradient that is compatible with web-based email clients such as Outlook and Gmail

Recently, I was given the task of creating a newsletter to support cancer fundraising. Excitedly, I accepted the assignment but encountered a frustrating issue with web-based email clients (such as outlook.com and gmail.com) not accepting some of my stylin ...

Tips for removing arrays and their values when a duplicate id is detected

Is there a way to remove or filter out subsequent arrays and their values if a duplicate id is found in the previous array? For instance: const a1 = ["id1", "b", "c", "d", "e"], a2 = ["id2", ...

Does a Function Component become an object instance of itself when rendered with a tag?

Suppose I declare a constant variable num = 1 inside a function component. Would it be possible for me to reference this variable in the return statement as this.num? Is it permissible for function components to contain static variables of that nature? ...

Is there a way to completely remove all styling from a specific child element?

Struggling with a drop-down menu issue - the parent element has a border, but I don't want that style to be inherited by the child sub-menu. Despite my attempts to remove the border using border: 0px, it's not working as expected. Is there any wa ...

Utilizing AngularJS to display numerous charts on a single webpage

Currently, I am looking for a solution to display multiple charts on the same page. My goal is to showcase individual performance through each chart for different persons. After exploring Google Charts, I noticed that it requires unique "id" attributes w ...

What is the best approach to modifying array elements using onChange and hooks in React?

I'm struggling to implement something simple, and the solutions I've found so far don't quite address my specific issue. Here's the state in question: const [formFields, setFormFields ] = useState({ formTitle: '', fiel ...

What is the method for invoking a component with an event in Vuetify?

Currently, I am working on implementing a SnackBar component on my website. The requirement is that the snackbar should appear when the user clicks the submit button. How can I pass my snackbar component to the button inside my Add.vue file? I want to be a ...

Using Three.js to display a CSS3D-rendered DIV in full-screen mode

After extensively experimenting with the three.js library, I was able to establish two distinct scenes – one canvas-rendered and the other css3d-rendered – both displayed using the same perspective camera. Note: Despite my efforts, I am constrained to ...

TimePicker Component for ASP.Net MVC with Razor Syntax

I'm currently working on implementing a TimePicker using Razor and JQueryUI in my bootstrap website. While I have successfully created a DatePicker, I am facing difficulties in creating a separate TimePicker using two different TextBoxes instead of se ...

Tips for incorporating a favicon in a React application

Having trouble adding a favicon to my React application. I followed the instructions in this post, but it's not working for me. I placed the favicon.ico file inside the public folder where index.html resides. This is how my directory structure looks ...

Create a custom implementation of Bootstrap's Typeahead feature using Angular, CSS, and HTML without relying on any pre-existing libraries

Is there a way to achieve bootstrap's typeahead suggestive hint feature using only angular, html, and css? I want to dynamically display the first item of the search results in the search box! <body> <div ng-app="demoApp" ng-controller=& ...

Weird errors popping up when running npm build in ReactJS - lifecycle and export issues arise

Currently facing an issue while trying to build a React project using npm run build for creating a production build Running npm start works perfectly fine and compiles the react code without any issues npm run build - error https://i.sstatic.net/lMfbK.pn ...

What strategies can I implement to prevent position: absolute from obscuring my content?

I am currently experiencing an issue with my Google Map loading within a tab. The container div has the position set to absolute. Although the map displays correctly, I am encountering a problem where it covers any content I try to place underneath it. My ...

Update D3 data, calculate the quantity of rows in an HTML table, and add animations to SVGs in the final

Attempting to update data in an HTML table using D3 has proven to be quite challenging for me. My task involves changing the data in multiple columns, adjusting the number of rows, and animating SVG elements in each row based on new data arrays. Despite tr ...

Trouble with incrementing JavaScript dictionary values within a nested for loop

I am currently working on analyzing shark attack data with d3.js using a csv file named shark.csv. I have encountered a problem while implementing a nested for-loop in my code. The csv file contains information about shark attacks categorized by continent ...

Exploring the eccentricities of browser rendering

Check out this JS Fiddle demonstration. Key Details: The div has a width of 500px The image inside is horizontally centered using CSS (margin: 0 auto) The image is wrapped in an anchor tag (with no additional CSS styling) Issue: Upon inspecting the a ...

How can I implement Google Analytics Event tracking for all <audio> tags using Javascript?

I am looking to implement a Google Analytics Event for all audio tags on my website. Currently, I have a script that targets audio tags with a specific class: <script> /* a test script */ $(function() { // Execute function when any element with t ...

The function of page-break-after: always is not functioning correctly

I want the Div with class questions_visual to appear on the second page when printing. <html> <head> <title>quiz</title> <link rel="stylesheet" href="style.css"> </head> <body> < ...

Leveraging the power of the Vivid.JS library with Internet Explorer

Recently, I stumbled upon Vivid.JS, an SVG Icons library that caught my attention due to its lightweight nature and customization options. The usage instructions are simple and straightforward: <i data-vi="icon-name"></i> Unfortunately, I enc ...