Repetitive notifications using Jquery

Utilizing the CSS and HTML code below to showcase a notification whenever an event takes place.

HTML

 <div id="notification_wrapper">
            <div id="notification"></div>
 </div>

CSS

#notification_wrapper 
{
    position: fixed;
    left: 50%;
    float: left;
    z-index: 1060;
}

#notification 
{
    left: -50%;
    float: left;
    position: relative;
    border: solid 1px black;
    padding: 10px;
    border-radius: 5px;
    box-shadow: 0px 0px 20px #000000;
    background-color: yellow;
    font-weight: bold;
    display: none;
}

jQuery

$("#notification").text("A message tobe displayed").fadeIn().fadeOut(5000);

The current issue is that when I trigger the jQuery code with a button click, the notification displays correctly and fades out after 5 seconds. However, if I click the button again within those 5 seconds, the second notification gets queued up and displays once the first one has faded out.

My desired functionality is as follows: Upon pressing the button, the notification should display for 5 seconds. If I press the button again within this timeframe, the first notification should immediately disappear and only the new one should appear.

For a demonstration, check out the FIDDLE attached. FIDDLE

Answer №2

To ensure the animation is currently active, you should include a check:

let is_animation_running = false;
$(":button").click(function() {
    if(is_animation_running) return;
    is_animation_running = true;
    $("#notification").text("Displaying a message").fadeIn().fadeOut(5000, function(){
       is_animation_running = false;
    });
});

Answer №3

Perhaps you can give this a try using the jquery .stop() method, like in this example: https://api.jquery.com/stop/, you can check out the code on this JSFiddle

var counter = 1;
$(":button").click(function() {
  $("#notification").text('Message: ' + counter + ' to display').stop().fadeIn().fadeOut(5000);
  counter++;
});
#notification_wrapper {
  position: fixed;
  left: 50%;
  float: left;
  z-index: 1060;
}
#notification {
  left: -50%;
  float: left;
  position: relative;
  border: solid 1px black;
  padding: 10px;
  border-radius: 5px;
  box-shadow: 0px 0px 20px #000000;
  background-color: yellow;
  font-weight: bold;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="notification_wrapper">
  <div id="notification"></div>
</div>
<input type=button value=click />

Answer №4

Here are the steps you should follow:

$(":button").click(function(){
$("#notification").stop();    
$("#notification").text("Display this message").fadeIn().fadeOut(5000);
});

Your fiddle has been updated with the new code.

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

Position the button at the bottom of the card using Bootstrap

While exploring a Bootstrap example utilizing the card-deck and card classes (Pricing example), I pondered over how to align buttons correctly when one list has fewer items than others. https://i.sstatic.net/IGN7K.png I aim to vertically align all button ...

Step-by-step guide for implementing an "on change" event for a select box within a dialog box

I recently wrote an HTML code snippet like this: <div id = "dialog-1" title = "Dialog Title goes here..."> <select id= "lang" name= "lang"> <option value="1"> TEXT </option> <option value="2"> HTML </op ...

How to send information to a modal component in ReactJS?

I'm feeling a bit lost here, maybe I'm missing something. What I am trying to achieve is a loop that populates an array with progress bar elements and then displays them with the relevant information. When a user clicks on a progress bar, the d ...

What is the best way to find the Selenium Webdriver element for the following HTML/jQuery snippet?

After numerous attempts with xpath, I am consistently encountering the noelementfoundexception error. My next strategy is to attempt clicking on Section 2. ...

Black screen issue with JW Player on iPad

I am currently using the JW Player to embed my videos and facing an issue with them not playing on iPads or iPhones. It seems like a problem with the HTML5 video tag as the screen remains black when trying to play the videos. Below is the code snippet for ...

Is it possible to postpone the loading of an element with the help of jQuery?

I've created an interactive accordion with a loading image overlay. Clicking on the image reveals the accordion content, and everything is functioning correctly. However, there's a brief moment when the raw contents of the accordion are visible b ...

Eliminate Dates from the Past - Jquery Datepicker

Currently, I am developing a booking system for my client. I have successfully disabled Sundays and Mondays (the days she is not open). However, I am now working on enhancing the functionality by blocking out past dates. Although I have written a function ...

What is the best method for sending data from an HTML page to a Django view in order to utilize it

I'm encountering an issue with Django urls. I need to pass the value entered in the Name input field after clicking the submit button. Let's assume I have the following HTML form- <form method='POST' action="{% url 'submittedf ...

Implementing Content-Security-Policy with React and Material-UI v4 for secure styling requirements

Utilizing a React UI with material-ui version 4, the web server is embedded within a JVM application during Production. Following npm build for the UI, the bundle is deployed alongside the server side code. An essential requirement involves implementing C ...

Limit the user's ability to choose just one radio button from each of two groups that have identical values

Currently, I am developing a web application that involves creating a virtual team. Users are able to choose players and then assign a "Captain" and a "Vice-Captain" for the team. To facilitate this, I have implemented two radio button groups where users c ...

Utilizing the jQuery Form Plugin for efficient form handling, incorporating Ajax File Upload for

Currently, I am utilizing the jQuery Form Plugin in a project involving a complex form with file fields. The backend framework being used is Ruby on Rails. Struggling with configuring Rails to recognize the POST request type as 'text/javascript,&apos ...

Adding a condition prior to inserting rows into a table using jQuery

Seeking advice on implementing an HTML template that includes a table and an add button. When the add button is clicked, selections are meant to be added to the table. The requirement is to introduce a condition where if one of the selections contains "Map ...

Limited access textbox

Is there a way to create a text-box in AngularJS/HTML that is partially readonly? For instance, having the default value as "+91" and making it readonly while allowing users to enter additional values afterwards? ...

Obtain the location information upon the loading of the webpage

I am currently in the process of developing a website using HTML, CSS, and JavaScript. My goal is to automatically retrieve the user's location when the page loads, without requiring them to click a button. I would greatly appreciate any assistance wi ...

Troubleshooting the pre-loader image loading problem

Apologies for bringing up a minor issue, but I'm struggling with this one. I added a pre-loader image to my page load using a simple method. You can find my page here Here is the HTML code for the Pre-loader image: <div class="se-pre-con">&l ...

I'm having trouble with my form submission using AJAX, as it's not echoing

I am currently testing the submission of a form using Ajax (submitting to my own page "new1.php"). What I am aiming for is to have the first and last name echoed after clicking the submit button. However, I am not seeing the first and last name displayed ...

Middle row positioning for button in Bootstrap 4 form-inline

My goal is to create a form with inline elements using Bootstrap 4. However, I'm facing an issue where the button always aligns at the top of the row instead of being in the middle like the input field. How can I ensure that the button's position ...

Show stored procedure query results in ASP.NET MVC

After spending over 8 hours attempting this task, I seek assistance. Please take note of the edit at the bottom. Implementation of Stored Procedure CREATE PROCEDURE SIX6 (@YEAR INT) AS BEGIN SELECT * FROM (SELECT Song ...

Is it possible for the client to see the PHP output within this JQuery script?

I have a page where users can contact me through a form. I want the message to be sent to an email address that my client can easily update. To achieve this, I've added a custom field (ACF) which is accessed using the following code: <?php the_fie ...

Send the user back to the homepage without the need to refresh the page

When the user clicks "Okay" in my window.prompt, the code below opens a new tab. Is there a way to direct the user to the home page without opening a new tab? if (window.confirm("Do you really want to leave?")) { window.open("./Tutoria ...