Execute the script repeatedly

(Since Stack snippet is temporarily down, I included a jsfiddle link): Visit this link for the code

I've been struggling to get this script to work. Here's the simple Javascript code I have:

$(document).ready(function() {
  showMessage();
  setInterval(function() {
    $(".msg-content").empty();
    showMessage();
  }, 7000);
})
:root {
  --msg-content-height: 26.75em;
  --msg-spacing: 1em;
  --msg1-height: 5.5em;
  --msg2-height: 4em;
  --msg3-height: 2.5em;
  --msg4-height: 2.5em;
  --msg5-height: 4em;
  --msg6-height: 3.25em;
}

/* CSS Animation Styles */

.msg-send {
  /* styles here */
}

.msg-receive {
  /* styles here */
}

.msg-container {
  /* styles here */
}

.msg-content {
  height: var(--msg-content-height);
}


/*Keyframes for Animations*/

@keyframes msg1 {
  /* keyframe animation details */
}

@keyframes msg2 {
  /* keyframe animation details */
}

/* Repeat keyframes for msg3 to msg6 */

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="msg-container pv4 ph4 br3 center bg-white shadow-6">
  <div class="msg-content w-100 relative">
    <div id="msg1" class="msg-receive dib mb4 bg-message br4 pv2 ph3 white measure-narrow">Message #1</div>
    <div id="msg2" class="msg-send dib mb4 bg-near-white br4 pv2 ph3 mid-gray measure-narrow">Message #2 </div>
    <div id="msg3" class="msg-receive dib mb4 bg-message br4 pv2 ph3 white measure-narrow">Message #3</div>

  </div>
</div>

The script almost works perfectly, but I'm having trouble figuring out how to hide the messages once they reach the designated height specified by --msg-content-height. Is there a way to make the messages loop continuously and disappear once they reach the top?

Answer №1

If you wait for the animation to finish before removing each message, you can easily delete them one by one. This technique is particularly useful in demonstrations where a delay between messages is desired for better presentation. You may need to fine-tune the keyframe percentages if necessary.

$('#msg1, #msg2, #msg3').on('animationend', function () {
    $(this).remove();
});
#msg1 {
  animation-name: msg1;
}

#msg2 {
  animation-name: msg2;
  animation-delay: 1s;
}

#msg3 {
  animation-name: msg3;
  animation-delay: 2s;
}

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

Create a configuration featuring filter options similar to Notion's functionality

The objective is to create a system that can establish certain constraints, similar to the way Notion handles filter properties. https://i.sstatic.net/plctW.png System A sets up the constraints and System C evaluates them, both using Typescript. However, ...

A guide on developing a Website Tour using jQuery

Exploring ways to create a website tour for first-time users? I've been searching for free website tour plugins and came across this one: http://tympanus.net/codrops/2010/12/21/website-tour/ However, the instructions provided are quite brief, making ...

What causes the input to be rendered as read-only when using the value attribute and as read-write when using the defaultValue attribute?

Within my shopping cart, the quantity of items added dynamically increases by +1 each time the same item is added. Additionally, users can manually adjust the quantity of an item within the cart to their preference. I encountered a dilemma with using the ...

Angular Headers API Development

https://i.sstatic.net/zQzAC.jpg I've been attempting to include the X-APIKeys headers in every api call, but I'm only finding examples in curl. Here's what I've tried: var accessKey = "gdgfdgdfgdgdgfdgdfgfdgfdgdgh"; var sec ...

Issue with upload progress bar functionality on Firefox for cross-domain uploads

I have a code snippet that serves as a cross-domain uploader (example.com) with a progress bar. $.ajax({ url: 'http://x.x.x.x:8080/.../uploader.php', type: 'post', ...

What is the best way to arrange these containers in a horizontal stack?

Check out the code snippet at: https://codepen.io/shayaansiddiqui/pen/YzGzeOj I'm looking to align these boxes horizontally, how can I achieve that? <div> <div class="container"> <img src="#" ...

What is the computational efficiency of accessing the size property of a Map in JavaScript?

I'm curious about the time complexity of this method compared to using .length on an array. Check out this link for more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size ...

Efficient Error Handling in Next.JS with Apollo GraphQL Client

Although the component successfully renders the error state, an uncaught exception is displayed in the console and a dialogue box appears in the browser. How can expected errors be handled to prevent this behavior? import { useMutation, gql } from "@a ...

Javascript unable to access SVG element content on mobile Safari

My approach to reusing certain SVG objects involves defining symbols in an SVG element at the top of my DOM. When I want to display an SVG, I typically use: <svg><use xlink:href="#symbol-identifier" /></svg> For animating SVG's, I ...

On mobile devices, the alignment of text in Bootstrap doesn't appear as intended

I'm creating a portfolio website for showcasing my design projects from university. The text in the "my work" section is centered, but it seems misaligned with other elements like buttons when the window width is reduced. What could be the issue? Cod ...

Utilize the client-side JavaScript file with ejs framework

Recently, I have been working on creating a website using Express and EJS. I discovered that using just one JavaScript file for all my EJS (view) files was causing issues. If I target a DOM element in one view page and it doesn't exist in another, I w ...

Separating the login/register functionality from the main app using the MEAN Stack

Apologies for my poor English! I have developed an application using the MEAN stack (MongoDB + Express.js + Angular.js + Node.js) with authentication utilizing passport.js and JWT (jsonwebtoken and express-jwt). What I aim to achieve? The login and r ...

Converting Rich Text Format (RTF) Data to HTML: A Step-by

I have information stored in RTF Format within an MS Access Database. The format is as follows: {\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\f ...

The JSON object encountered a TypeError due to a null element

Utilizing an Ajax POST method to retrieve results from a cross-domain source. Upon execution of the following code: var results = JSON.stringify(data); console.log(results); The returned data is as follows: {"ERR":null,"RSP":{"FLDR":{"ID":"1","TJT":"FUL ...

Changing the background color of required inputs that are empty using jQuery

I need help customizing the background color of required input fields that are left empty when a user submits a form. Some of the fields are optional, but I want only the required ones to be highlighted in red. Specifically, the required fields have been ...

What method is most effective for duplicating objects in Angular 2?

Is it just me, or does Angular 1.x have methods on the global angular object like angular.copy and angular.shallowCopy that are missing in Angular 2? It seems like there is no equivalent version in Angular 2 documentation. If Angular 2 doesn't plan on ...

Python's Selenium execute script does not support setTimeout function

Hey there! I was attempting to pause the Selenium execution for a few seconds in order to wait for a Modal popup to appear. Unfortunately, using time.sleep(5) did not work with PhantomJS (apparently, PhantomJS does not support sleep function). So, I decide ...

What is the best way to transfer a string to a different Vue component?

Within my project, I have a masterData.js file that serves as a repository for my master data. This file retrieves data from my mongo db and distributes it to various components of the project. To facilitate this process, I have implemented a function with ...

Bundling extraneous server code in client rollup

Can someone help me understand why the following code is being included at the top of my browser bundle by Rollup? It seems like these references are not used anywhere from my entry point. Could it be default includes from node builtins? import require$$0$ ...

What is the best way to change the color behind the SVG to black?

I'm having some trouble creating a signup form with SVG logos. The colors seem to clash and no matter what I do, the color doesn't change from white. My goal is to have the actual SVG logo remain white while the background behind it is black. He ...