Postpone the execution of the toggleClass function

I have a tab that, when clicked, fades in to reveal content loaded with AJAX. However, the content loads immediately upon clicking the button. I attempted to use toggleClass with delay, but it was unsuccessful.

How can I effectively delay the loading of the content? This is the HTML :

$("#button-1").on("click", function() {
  $(".hidden-content-1", 2000).toggleClass("show-hidden-content", 2000);
  $(".main-page-content-1", 2000).toggleClass("hide-shown-content", 2000);
})
#modal-1 {
  width: 33.33%;
  height: 100vh;
  background-color: green;
  left: 0;
  top: 0;
}

.modals {
  transition-timing-function: ease-out;
  transition-duration: 1000ms;
  position: absolute;
}

.active {
  width: 100vw !important;
  height: 100vh !important;
}

.show-hidden-content {
  display: block !important;
}

.hidden-content-1 {
  display: none;
}

.hide-shown-content {
  display: none !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="modal-1" class="modals">
  <div class="hidden-content-1">
    <h1> TEST </h1>
  </div>
  <div class="main-page-content-1">
    <h1>TEST </h1>
  </div>
  <a id="button-1" href="template-parts/panel1.php"><input onclick="change1()" type="button" value="See More" id="button-text-1"></input>
  </a>
</div>

Answer №1

Here is a sample code snippet that may be of interest:

$('#button-1').on('click', function () {
  setTimeout(() => {
    $('.hidden-content-1').toggleClass('show-hidden-content');
    $('.main-page-content-1').toggleClass('hide-shown-content');
  }, 2000);
});

Answer №2

Consider incorporating animation-delay for timing

#target {
    animation: fade-in 250ms ease-out 1s 1 normal both running;
}

@keyframes fade-in {
    0% {
        opacity:0;
    } 100% {
        opacity:1;
    }
}

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

Exploring the use of asynchronous data retrieval with jQuery and JSON within MVC 2.0

Attempting to retrieve server-side data using jQuery's getJSON method has hit a snag. The URL specified in the getJSON call is being reached, but the expected result is not being returned to the browser upon postback. There are suspicions that the iss ...

Tips for simplifying a JavaScript function

Hello everyone, I just joined StackOverflow and I could really use some assistance with a JavaScript and jQuery issue that I'm facing. Can someone suggest a more efficient way to write the code below?: jQuery(document).ready(function () { $("#ar ...

Tips for handling line breaks across different platforms within a web application

I am currently developing a Web application and I have encountered some difficulties with the handling of newLine characters. My application allows users to create posts (similar to a forum) using the Markdown language. This means that the content is in p ...

Difficulty with wp_enqueue_script function

Issue: My custom.js is loading before jquery causing an error. In my functions.php file, I have included the following code: function blue_sentry_scripts() { wp_enqueue_style( 'blue-sentry-style', get_stylesheet_uri() ); wp_enqueue_scr ...

Delete an entry from the list

I have a fully functional Ajax call and function that fills up a datalist. This datalist is utilized to choose from a limited list and add it to a UL element on the page. Once an option is added from the datalist to the actual list, I aim to eliminate that ...

Getting row data in datatables after a button click: A step-by-step guide

Could somebody provide assistance with retrieving a single row of data on a click event? This table is dynamically populated after the AJAX call's Success function is executed <div class="table-responsive table-wrap tableFixHead container-flu ...

The iframe is set to take the source URL from url.com/id using the "id" parameter

Currently, I am attempting to set the src of an iframe by extracting the URL from the toolbar address. The URL consists of the domain plus an ID, which happens to be the same as the YouTube ID. For instance: www.youtube.com/watch=id In my case, my domain ...

Display an image using a modal window and Ajax XMLHttpRequest

I was tasked with creating a button that can load various types of content (text, images, videos, etc) into a modal popup window using Ajax without any frameworks. So far, I've been successful with text but have run into issues with loading images. De ...

CSS animation keyframes failing to initialize

How can I fix my CSS @keyframe animation that is not starting as expected? http://jsfiddle.net/sadmicrowave/VKzXp/ @-webkit-keyframes slideNotificationsHide { 0%{ width:70%; } 100%{ width:94%; left:0; right:0; } } #left-container{ position:abosl ...

Determine the viral coefficient based on the sharing platform being used, whether it is Facebook or Twitter

Traditionally, the viral coefficient is measured through email addresses. For example, if a current user [email protected] invites 10 people via email, you can track how many signed up and calculate the viral coefficient based on that. But what if th ...

How can I style an image in CSS to have a set height, maximum width, and keep its aspect ratio fixed?

I am trying to place a set of images with varying heights and widths inside a div tag. My goal is to have the images maintain a fixed height, unless the width surpasses a certain limit, in which case I want the height to adjust while preserving the aspect ...

What is the method for assigning a maximum value of 300 to a bootstrap progress bar?

I'm in the process of creating a progress bar with a maximum value of 300, but I'm struggling to set it at 300. It currently works for a maximum value of 100, but I want it to be 300. Here's what I've attempted: <div class="mt-3 pr ...

Using AJAX in jQuery to toggle the visibility of rows in a table with the each

Currently, I am working on an MVC 5 application where I am attempting to utilize a script to toggle the visibility of buttons in each row based on data retrieved from a database using Ajax. function setButtons() { $('#MyList > tbody > ...

Despite utilizing overflow and wrap properties, HTML lists still fail to display horizontally

Make sure to copy the code and run the HTML file first. For organization, place the JavaScript file in the scripts folder and the CSS file in the styles folder. The issue at hand is that the li elements are not displaying horizontally as intended; they n ...

Issues with pop-up windows on YII2 gridview

I am currently developing a web application using the Yii2 framework for my company to efficiently manage their storage. I have incorporated various plugins from kartik-v to enhance the functionality of the application. However, I am facing an issue with i ...

Exploring each item within oData: A guide

After writing the code statement above, I am able to see the attached image. Now, my challenge is accessing the "label" property inside each object. How can I iterate through these objects and retrieve their "label" properties? item.getModel().oData; I a ...

Newbie's Dilemma: Issues with Html, Email, and Distribution Lists

I am looking to add a new feature to my website where users can enter their email address to receive a free promotion and be added to an announcement list. Since I don't have any experience with web programming, I'm not sure where to start. Would ...

Tips for sending a unique button ID to a jQuery click function

Within a table row of a dynamically generated table, I have multiple buttons each with its own functionality. My goal is to figure out how to pass the specific button ID to the onclick event of that table row when one of these buttons is clicked. $(&apos ...

PHP code that generates a drop-down list using a foreach iteration

all I'm encountering a slight issue with trying to create a dynamic drop down list. Here's the code I've written: <select name='categoryID' > <?php foreach( $categories as $category)?> <option value="<?p ...

Discovering the offset location using touchmove

Struggling with a code for dragging/moving an element via touchscreen. The code is functional, but encounters a common issue - unable to drag from the touchpoint itself. Every movement initiates from the edge of the element, no matter where the touch begin ...