Incorrect sequence detected in JQuery animation execution

I'm currently developing a game called 'Pointless', inspired by the popular UK game show of the same name. In the game, there is a countdown that starts at 100 and decreases to display the team's score. I'm attempting to recreate this feature in my game. For reference, you can watch a clip from the show here.

However, I'm encountering an issue where the entire countdown executes simultaneously instead of revealing one element at a time as intended. I need to figure out how to hide each div separately.

You can view the code I'm using on this JSFiddle.

for (var i = 0; i <= 10 ; i++) {
    $('#' + i).toggle('slide');
}

Answer №1

When using jQuery's `toggle` or other animation functions, the code does not get blocked. The animation runs concurrently with the rest of the code execution. To introduce a delay before starting each block of animation, you can utilize the `delay` function.

For example, check out this demo:

Moreover, I recommend using .slideToggle('slow') instead of .toggle('slide').

$('#' + i).delay(i*100).slideToggle('slow');

Answer №2

After running through the entire for-loop, it is discovered that the .toggle() events (including others) are actually enqueued and triggered. This means that even if they were not, calling toggle on all elements simultaneously will result in them toggling at the same time. One workaround is to implement a timer using functions like setInterval or setTimeout:

$('#Go').click(function(){
  var i = 0;
  var timer = setInterval(function(){
      i++;
      $('#' + i).toggle('slide');
      if(i > 10) clearInterval(timer);
    },100)
})

Check out this Fiddle Example

Answer №3

Amir's solution is excellent. I just want to make a small addition:

The toggle method in jQuery does not accept any argument with the value of slide. Here is the correct syntax:

$(selector).toggle(speed, easing, callback)

speed: [milliseconds, "slow", "fast"]
easing: ["swing", "linear"] (Additional easing functions can be found in external plugins)
callback: a function

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

Creating a new column in a DataTable dynamically after an Ajax request in an MVC environment

In the view page, there is a table displayed from a partial view along with a form which is being submitted using ajax to a webapi controller. Upon successful submission of the form, the goal is to add the entered text from the textbox to the first column ...

What is the process for adding a JavaScript banner using JavaScript code?

I would like to insert a javascript ad banner using code. Here is the current code snippet: <script> document.write("<script src="http://adsite.com"></script>"); </script> Unfortunately, it doesn't seem to be functioning p ...

Node.js MySQL REST API query fails to execute

I am developing a login/sign up API using nodejs, express, and mysql. Despite receiving the "Successful Sign Up!" message without any errors during testing, the user table in the database remains empty. Below is the query I am attempting to execute: con. ...

What could be causing wavesurferjs to exceed the boundaries of its parent div?

I need assistance with my wavesurferjs issue The waveform is overflowing the parent div This problem occurs for the first time and upon resize of the parent div Upon resizing, it should automatically adjust to fit the parent div Question: When the pare ...

Troubleshooting Problems with Jquery Datatables Resetting

Whenever I click the button, a datatable is populated with values from the server. It acts as a search functionality, but when I change the search text box value and click for the second time, the changes are not reflected. The second time, the datatable ...

Having trouble getting the JavaScript slider to animate

My code works perfectly in Codepen but when I try to implement it on my website, it shows as a static image. You can view the code here: https://codepen.io/anon/pen/zPMKpv I'm using the exact same code on my website located at: Does anyone have any ...

Are backbone models causing hiccups in productivity?

Currently, I am working on a GPS data visualizer project using Backbone. Each GPS datum is stored in a backbone model and all of the data is saved in a collection. I am curious to know the kind of overhead involved in this process compared to using an arra ...

Using jQuery to modify a CSS property when a specific option is chosen

I'm currently working on creating an option dropdown menu and I've hit a roadblock trying to display a textbox only when the 'other' option is selected. Here's what I have so far: <span id="con-country"><label for="count ...

Enhancing OpenAI API Responses with WebSocket Streaming through Express Middleware Integration

  Currently, I am in the process of developing an Express.js application that requires integration of OpenAI's streaming API responses to be transmitted in real-time to a front-end via WebSockets. Even though I have established communication between ...

Hiding and displaying DIVs on a single HTML page using VueJs 2

I am currently working on building an application that is not a single page application. As I develop my project, I have several div elements on the page that I want to toggle visibility step by step. Below is the snippet of my code: <div v-if="sect ...

The onChange function does not seem to be functioning properly within the Reactstrap customized input switch

Here is some code: import { CustomInput } from 'reactstrap' ... const changeMediaStatus = (e) => { console.log(e) } ... <CustomInput type="switch" className="ml-auto mr-1" onChange={(e) =>changeMediaStatus ...

Error: 'require' is undefined in react.production.min.js during production deployment

Greetings! I am encountering some difficulties while trying to build on production: the error "require is not defined" is being caused by react.production.min.js. Below are my webpack.config.js and package.json files: webpack.config.js const path = requi ...

The background of the webpage section is being cropped on mobile devices

Currently, I am delving into the creation of a website completely from scratch to serve as an online CV for networking and job/school applications. My journey with HTML and CSS is relatively fresh, having started only about 5 months ago. Overall, things ha ...

Styling a Fixed Header in CSS/HTML - Scroll Effect

Is it possible to hide only the upper part of the nav-bar, similar to the behavior in WhatsApp? I am using material-ui for this particular use-case. Currently, my implementation causes the app-bar to extend only when the scroll position is less than 48px, ...

What is the best way to showcase a non-image file that is stored in MongoDB (using GridFS) on a webpage?

I have a project where users can upload documents (not images) that are stored in MongoDB using GridFS. My question is, is there a way to display these documents on the site for viewing instead of just downloading them? I've tried using the createRead ...

Learn the simple process of resetting a form with the input type reset functionality

Hello! This is my first time asking a question here, so please bear with me if I make any mistakes. I am attempting to reset a form using the input type=reset tag in HTML5, but I'm encountering some issues. I've included a snippet of my code bel ...

A row divided into three segments in Twitter Bootstrap2, with the second segment spanning the rest

Looking to divide a row into three segments under the following conditions: The first segment should have a maximum height and always stay at the top (minimum height of 0; maximum height of 40%). If the content exceeds the max-height, a scroll bar sho ...

Is it possible to simultaneously run multiple functions with event listeners on a canvas?

I'm attempting to create a canvas function that displays the real-time mouse cursor location within the canvas and, upon clicking, should draw a circle. I came across this code snippet that reveals the x and y coordinates of the mouse: document.addEve ...

Center sidenav material 2 in alignment

Using Angular Material 2 presents a challenge for me: While I can align the text in the sidenav to center, I am struggling to align the button correctly. https://i.sstatic.net/vjHw4.png <md-sidenav-layout fullscreen> <md-sidenav #start mode=" ...

Center php div within a form

Having some trouble with my php form and css code. I'm attempting to adjust the layout so that the message field appears on the right side. I tried placing the Name and Email fields in one div, and the Message and Submit fields in another, then both i ...