Tips for adjusting the duration of a background in jQuery

jQuery(document).ready(function(){
    jQuery('div').css('background-color','#ffffff').delay(12000).css('background-color','#000000');
});

Hello everyone! I'm having some trouble with this code. I am attempting to change the background color of a div element after a certain amount of time has passed, but it doesn't seem to be working as expected.

Answer №1

One possible solution is to use the setTimeout method.

jQuery(document).ready(function(){
    var elements = jQuery('div').css('background','#fff');
    setTimeout(function(){ elements.css('background','#000'); }, 12000);
});

Answer №2

Using only CSS, you have the ability to achieve this.

Example of CSS Animation

 body {
  animation:color-change 5s 1;
  -webkit-animation:color-change 5s 1; /* Compatible with Safari and Chrome */
  animation-fill-mode: forwards;
  -webkit-animation-fill-mode: forwards;
}

@keyframes color-change
{
  0% {background:#fff;}
  90% {background:#fff;}
  100% {background:#000;}
}

@-webkit-keyframes color-change /* Safari and Chrome */
{
  0% {background:#fff;}
  90% {background:#fff;}
  100% {background:#000;}
}

To customize the animation effects, simply adjust the properties as needed.

CSS Animation Browser Compatibility Information

Answer №3

When it comes to using .delay(), keep in mind that it only functions with jQuery's standard effect queue or a custom one. In most cases, opting for a setTimeout() call would be more suitable.

jQuery(document).ready(function(){
    jQuery('div').css('background','#fff')
    setTimeout( function(){ jQuery('div').css('background','#000'); }, 12000);
});

Take a look at this jsFiddle example (with a three-second timeout for quicker display)

Answer №4

When using the jQuery delay function, it is important to queue subsequent functions in order for them to execute properly:

$('div').css('background','#fff').delay(12000).queue(function(nxt){
     $(this).css('background','#000');
     nxt(); // continue the queue
 });

For a visual demonstration, check out this jsfiddle demo

I hope you find this information useful.

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

Is there a tool that can help pinpoint the original collection for shared elements across multiple collections?

When given multiple collections/arrays, I am interested in identifying the common elements and which collections they belong to. This request is somewhat similar to this query, but there may be matching elements between collection1 and collection3, or eve ...

Using React Native's stylesheet to apply multiple values in a CSS statement

Here's a scenario to help clarify my question: Imagine I want to add margin to an element in the following way: const myView = () => <View style={styles.viewStyle}></View> const styles = StyleSheet.create({ viewStyle: { margin: ...

What is the process of acquiring JSON and converting it into a JavaScript object array?

Can someone please assist me in converting the JSON generated in Spring Boot into a JavaScript object array? I'm currently facing some difficulties. https://i.stack.imgur.com/Tx5Ri.png I've been using XMLHttpRequest and my JS code is as follows ...

To make a JavaFX label stand out, simply prepend a vibrant red asterisk at the start of

I'm facing a challenge with my SceneBuilder project. I have developed a scene that consists of several labels, and I want to highlight some of them by adding a red asterisk at the beginning of their text. Unfortunately, I haven't been able to fin ...

The scroll functionality does not seem to be functioning as intended on mobile devices when

Hey there, I'm currently working on making my navbar sticky and applying it when the page is scrolled. The code I have works flawlessly on desktop, but unfortunately, it doesn't seem to work on the mobile version. Any suggestions? window.addE ...

Why does Vue continuously insert undefined values when adding non-consecutive indexes to an array?

In my application, users can select values from a dropdown list and add them to an array by clicking the "add" button. The goal is to use the selected value's id as the index in the array. For example: List of Values 1 - Apple 3 - Bananas 8 - P ...

Utilizing Bootstrap 4 for a responsive layout: positioning a <div> element adjacent to a centered image

I am currently working on a project involving an image gallery. My goal is to showcase the images in a specific layout: https://i.sstatic.net/cXcql.png Essentially, the image should always be centered. If there is sufficient space to the right, I want to ...

Guide on sending files and data simultaneously from Angular to .NET Core

I'm currently working on an Angular 9 application and I am trying to incorporate a file upload feature. The user needs to input title, description, and upload only one file in .zip format. Upon clicking Submit, I intend to send the form data along wit ...

Ways to eliminate white space in bootstrap CSS menu bar

My goal is to create a responsive navigation bar using Bootstrap and customize it to fit my design needs. Although I was able to implement the dropdown on mobile-sized windows, I encountered a margin issue that I couldn't remove on the right side. I ...

Is there a discrepancy in the JSON algorithm?

While using google chrome, I encountered the following scenario: I had an object in javascript that looked like this: var b = { text: 'hello world, you "cool"' }; I then used JSON.stringify to convert it to a string and sent it to the dat ...

avoid loading javascript in ajax layer

After struggling for days to find answers online and unsuccessfully trying different codes, I discovered a way to use ajax to dynamically change the contents of a div element and display different sliders. Here are the files if you want to take a look: Fi ...

Arrange, Explore (Refine), Segment HTML Chart

Is there a way to efficiently sort, paginate, and search (based on a specific column) data in an HTML table? I've explored several JQuery plugins such as DataTable, TableSorter, Picnet Table Filter, but none seem to offer all three functionalities (se ...

Learn the process of including an active class using jQuery from an external active.js file!

This JavaScript code snippet $(function() { $("#navbar-nav li").click(function($) { // remove classes from all $("li").removeClass("active"); // add class t ...

Max value determined by a variable within a for loop

UPDATE: Revised code provided. In my Node.js JavaScript project, I am creating a script to iterate through a dataset obtained by web scraping. The main goal of this algorithm is: 1) Examine the player table for the presence of the player's name in a ...

Integrating array elements into the keys and values of an object

Given the array below: const Array = ['Michael', 'student', 'John', 'cop', 'Julia', 'actress'] How can I transform it into an object like this? const Object = { Michael: student, John: cop, Juli ...

Two separate Bootstrap dropdowns are failing to function independently from each other

Can anyone help me prevent both buttons from displaying a drop-down when only one is clicked? https://i.stack.imgur.com/nvtbm.png Below is my HTML code: <form action="{{ url_for('crudSEapi.gcp_image_search') }}" method="post&q ...

An undefined variable in Angular 2's evaluation

I am currently working with the following code: @ViewChild('MondayPicker') MondayPicker; @ViewChild('TuesdayPicker') TuesdayPicker; @ViewChild('WednesdayPicker') WednesdayPicker; @ViewChild('ThursdayPicker') Thursda ...

What is the best way to engage in conversations with several users using socket.io?

Currently, I am in the process of developing a chat application with authentication. The implementation involves socketio for real-time communication and jwt along with cookies for user authentication. The connection to the database has been successfully e ...

Generating personalized MongoDB collections for individual users - A step-by-step guide

My query is more about the procedure rather than a specific coding issue. I am working on a node application and using DHTMLX calendar. What I aim for is to have each user with their own set of events on their individual calendar. Currently, the implement ...

Creating a JavaScript function in Selenium IDE specifically for today's date

Just starting out with Selenium IDE and looking to build a set of regression scripts for our department. Trying to insert today's date plus 180 days into a field using a JavaScript function. If anyone can guide me on how to write this function, I wou ...