Utilize AngularJS to enable a CSS Class through an array order

I need to activate a heartbeat CSS animation on 6 different divs, but I want to do it in a specific sequence.

For instance, let's say I have an array:

self.arraySequence = [0,3,5,3]; 

In this array, each number represents the position of the div that should receive the heartbeat animation.

https://i.sstatic.net/2L5yC.png

These divs are UI components created using AngularJs:

<div class="grid-cell"
    ng-class="{'heartbeat': $ctrl.isActive
    }"
></div>

I am attempting to use the controller to activate the HeartBeat animation sequentially at each position. So following the array mentioned earlier, it would look like this:

  • The div at position 0 will animate for 1 second
  • The div at position 3 will animate for 1 second after the div at position 0
  • The div at position 5 will then animate for 1 second after the one at position 3
  • Finally, the div at position 3 will animate for 1 second after the one at position 5
";

This way, all animations will take a total of 4 seconds.

I attempted using $timeout, but both animations executed simultaneously.

https://codepen.io/guifeliper/pen/pwdKKj

Answer №1

If you are looking to run a recurring function, it's best to utilize $interval over $timeout as the latter only runs once.

$ctrl.stopInterval = $interval(function () {
  $ctrl.heartbeatId = $ctrl.generatedSequence.shift();

  // Remember to clean up after 
  if($ctrl.generatedSequence.length == 0) {
    $interval.cancel($ctrl.stopInterval);
  }

}, 1000);

To update your grid cells with the appropriate classes, modify your ng-class directives in this manner:

<div class="grid-cell" ng-repeat="grid in $ctrl.gridBox"
    ng-class="{'heartbeat': $ctrl.heartbeatId == grid }"></div>

Visit this link for more details.

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

Show alerts that automatically disappear after a set amount of time

I have successfully implemented code that displays alerts for a specific period of time, indicated by (alert alert-warning). Additionally, I want to display another type of alert, (alert alert-success), for a certain amount of time, after which the page sh ...

Is it possible to create a resizable menu without distorting its appearance?

After setting up a menu on my website, I noticed that when resizing the window, it spills over into other divs on the same line. Despite trying multiple solutions, I have been unsuccessful in resolving this issue. The navigation for the menu is contained w ...

What is the highest value that AutoCompleteExtender can reach?

I am currently using an AutoCompleteExtender in my code with the following configuration: <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoCompleteOrigem" TargetControlID="txtClienteOrigem" ServicePath="~/Cliente/Au ...

Adding data to each span and div using JavaScript is a simple task that can be achieved easily

What is the best way to add information to each span and div element using JavaScript? $(document).on("click",".selection-state",function(){ stateid = $(this).attr("rel"); $("#my_tooltip").html(data); } e ...

Automate Zoom join function with the help of puppeteer

Having trouble joining a Zoom meeting using Puppeteer, my code is not capturing the password field. Can anyone assist? Here is my code snippet: const puppeteer = require("puppeteer-extra"); const StealthPlugin = require("puppeteer-extra-plu ...

jQuery sorting function is utilized to organize a table after its rows have been modified through an ajax

My website features an election section that displays updated election numbers in real-time. Previously, users had to refresh the page to see new results. I have implemented code that utilizes a custom REST API to fetch live numbers and jQuery to dynamica ...

Update Javascript variable every second

I have a script that retrieves the value "average" from a JSON array <p id="ticker"></p> <script> var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function () { myObj = JSON.parse(this.responseText); document.ge ...

When setting the request header in XMLHttpRequest, the $_POST variable will be null

I encountered an issue while trying to send a request using XMLHttpRequest in JavaScript code with PHP. Whenever I set the request header using `setRequestHeader`, the `$_POST` value returns null. However, if I remove `setRequestHeader`, the `$_POST` data ...

What is the method for deactivating a form input field in Angular Formly?

Is there a specific way to disable a form input field using Angular Formly? I have tried searching online and cannot find a clear answer. Even after checking Formly's 'field configuration docs', I only found one mention of the word 'dis ...

Issue with Jquery addClass method in Firefox

I am encountering an issue with the jQuery addClass() function in Firefox where the class is not being added to the current element. Interestingly, this code works perfectly in Chrome and Safari (IE has not been tested). Here is the CSS snippet : $(doc ...

I am having trouble viewing elements located below a div that I created using CSS

Currently, I am working on creating a portfolio page where the initial page opens with a height of '100vh' and width of '100%', which seems to be functioning correctly. However, I am facing an issue where I am unable to scroll down to v ...

Displaying the Ionic loading spinner on the entire page rather than a restricted small box

Right now, I'm able to implement a basic loading effect, but it appears as a small box. presentCustomLoading() { let loading = this.loadingCtrl.create({ content: 'Loading Please Wait...' }); loading.present(); } However, I ...

Issue with Local Storage: Value not being saved, instead [object MouseEvent] being stored

I am truly grateful for the help from @zim as it allowed me to drastically simplify my code for 2 buttons that store true/false values locally. However, I am facing an issue where the button click is registering as [object MouseEvent] instead of True/False ...

The POST Method is failing to transmit all the necessary data

I recently started a new project using angular, node, express, and postgresql/sequelize. It's been an exciting learning experience, but I've encountered some challenges. Specifically, I'm having trouble setting up an update route for a model ...

Inserting blocks of text into a MySQL database

I'm hoping to insert text segments into a MySQL database. Below is an excerpt of my HTML code: <div class="margins3"> <h1>Meal Plan...</h1> <div id='results-container'> <div class='LeanMuscle ...

What is the method for transforming an array object into an object?

How can I assign the values of an array object called hello to a constant called answer, changing the key value in the process? I've considered using Object.entries but couldn't quite get it right. Is there a way to achieve this? Here is my cod ...

The functionality of images and links is compromised when they are assigned as values to properties within a JSON object

My images and links are not working, even after declaring them globally. When I write the src directly into src, everything seems fine but the alert pops up with the same URL and img src. Can anyone help? var combo0; var combo1; var combo2; var combo3; ...

What is the method for activating or deactivating an <input> range with a <label> text?

I want the range to automatically switch between minimum and maximum values when I click on the label. Can this be achieved with just HTML and CSS or will I need some JS? Below is the code snippet I am working with: <label> This is the label ...

ReactJS Antd displays a faulty design on the webpage

Currently, I am utilizing Antd for a ReactJs project, however, I have noticed an issue with the rendering of components in the layout. Below is the code snippet: import React from 'react'; export default class Test extends React.Component { ...

"Implementing automated default values for Select/dropdown lists in ReactJs, with the added capability to manually revert back to the default selection

After browsing multiple websites, I couldn't find a clear solution on how to both set and select a default value in a select element. Most resources only explain how to set the value, without addressing how to reselect the default value. My Requireme ...