straightforward pop-up alerts

I need a way to incorporate simple toastr (notification) functionality without using the toastr plugin.

After 5 seconds, I would like to remove the transitioned class from the most recently added element.

var loadNotification = function(typeClass, message) {
    var notification = {
        typeClass: typeClass || 'alert-success',
        message: message
    };

    var template = UTILS.getHandleBarTemplate('#template-notification');
    var templateMarkup = template(notification);
    $('.notifications-area').append(templateMarkup);


    $('.notifications-area').find('.q-notifications').last().addClass('transitioned');
};

Current Implementation:

View Notifications Gif

Answer №1

To remove a specific class using the `setTimeout()` function in JavaScript, you can combine it with the `removeClass` method. Here's an example: Add the desired class to your element, then immediately call `setTimeout()` for 5000 milliseconds (5 seconds) to remove the class.

var loadNotification = function(typeClass, message) {
    var notification = {
        typeClass: typeClass || 'alert-success',
        message: message
    };

    var template = UTILS.getHandleBarTemplate('#template-notification');
    var templateMarkup = template(notification);
    $('.notifications-area').append(templateMarkup);

    $('.notifications-area').find('.q-notifications').last().addClass('transitioned');
   
    setTimeout(function() {
        $('.notifications-area').find('.q-notifications').last().removeClass('transitioned');
    }, 5000);
};

Answer №2

After encountering unpredictable behavior with removing the last element of notifications, I implemented a solution by assigning an ID to each notification and then removing that specific element after a set interval. This approach ensures that only the intended notification is removed without affecting others.

const notificationId = $('.notifications-area').find('.q-notifications').length;
    const notification = {
        typeClass: typeClass || 'alert-failure',
        message: message,
        notificationId: notificationId
    };

    const template = QUBOLE.UTILS.getHandleBarTemplate('#template-notification');
    const templateMarkup = template(notification);
    $('.notifications-area').append(templateMarkup);

    // Applying transitioned class quickly to avoid css transitions
    setTimeout(() => {
        $('#notification_' + notificationId).addClass('transitioned');
    }, 0);

    setTimeout(() => {
        $('#notification_' + notificationId).removeClass('transitioned');
    }, 5000);
    setTimeout(() => {
        $('#notification_' + notificationId).remove();
    }, 6000);

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

What are the top recommendations for implementing typography in SCSS?

As I ponder the best approach to take, I find myself torn between two options and wondering what the 'best practice' in this situation might be. My objective is to implement a selector that removes the standard global margin of 20px that I have ...

Integrate several YouTube players within a tabbed angular-ui system. Looking to incorporate a collapse feature

I come to you with a humble request for guidance. As a newcomer to Angular and someone who has never ventured into the realm of the YouTube JavaScript API, I am well aware that I may be making errors without even realizing it. While my code appears to be f ...

Encountering difficulties accessing Node.JS Sessions

Hey there, I am currently working on integrating an angular application with Node.js as the backend. I have set up sessions in Angular JS and created my own factory for managing this. Additionally, I am utilizing socket.io in my Node.js server and handling ...

Unzipping a zip file using the Command Line

I'm encountering an issue with the JSON code not working on the server. It appears that this problem may be due to Node.js being version 14 on Heroku, while the latest version is 16. Everything runs smoothly on my local computer but throws an immediat ...

The console is reporting an error stating it cannot read the charCodeAt property

<script> var str=prompt('please enter a string'); var a= str.split(''); for (j=0; j<str.length; j++){ for(i=j; i<str.length; i++) { if(a[i].charCodeAt(0) > a[i+1].charCodeAt(0)) { var b= a[i]; a[i]=a[i+ ...

Is there a way to eliminate the button's background color when clicking on an area outside of it

I recently designed a search bar with a category dropdown button, and managed to successfully customize its hover, active, and focus states to remove the background color. However, I encountered an issue where clicking outside the button to close the dropd ...

An uncaught exception has occurred: An error was encountered indicating that the specified path is not valid for either posix or windows systems, and it appears that there is no 'join' method defined in the

I am currently working with nextjs version 13.5.6 using the app router and app directory. This issue arises during the compilation of the route app/(home)/page.js. The folder and file structure within the app folder is as follows: app/ -(home)/page.js -ser ...

Scanner (IE5) impact on page load speeds

I have developed an MVC project which is designed as a single-page application (SPA) utilizing knockoutjs and sammy frameworks. The following script is placed in the head section of the page: <script> var startTime = (new Date()).getTime(); </ ...

Click outside to toggle dropdown and hide it

My dropdown menu is currently working, but I would like it to close when I click outside of it. I've tried various solutions provided in other posts, but I just can't seem to get it right. Can someone please help me figure out what I am missing? ...

Storing user input with AngularJS

I have a form below that I am trying to save. Please review my HTML code for the form. HTML <form ng-submit="save()"> <input type="text" ng-model="user.name"/> <input type="text" ng-model="user.age"/> <input type="text" n ...

Is it possible to invoke a helper function by passing a string as its name in JavaScript?

I'm encountering a certain issue. Here is what I am attempting: Is it possible to accomplish this: var action = 'toUpperCase()'; 'abcd'.action; //output ===> ABCD The user can input either uppercase or lowercase function ...

Using jQuery to remove the functionality of a file input button is ineffective

I am having trouble with an input type file element: <input type="file" name="val1" /> And I'm using this jQuery code: $("input[name='val1']").off("click"); However, the above script, which is already included in a $(function() { } ...

Storing Dark Mode Preference in Local Storage using JavaScript

I recently implemented dark mode on my website and it's working flawlessly. However, every time I refresh the page, it reverts back to the default Day Mode view. Is there a way to save these preferences? In the HTML section: <body> &l ...

jQuery - Resolving the Conflict

My current challenge seems to be related to the scope of my approach. I've noticed that either the slider works, or the images replace text, but not both at the same time. The inclusion of the "scrollbox.min.js" file is necessary for the slider to f ...

The process is consuming a significant amount of time to generate the expected result

My code is currently running all the queries inside a while loop and it's taking around 1 minute to complete. I need suggestions on how to optimize it for faster performance. Here is my code: <table style="font-size:13px" width="150%" border="0" c ...

The optional chaining feature seems to be malfunctioning as it is unable to access the property '0' of an undefined value

Why isn't optional chaining working in this scenario? .html {{userItemModel?.item?.priceList[0]?.sellerUrl}} An error is displayed: TypeError: Cannot read property '0' of undefined "@angular/core": "~10.1.1", "t ...

The node path.relative function is providing inaccurate path information

It seems like there might be an issue with the output of path.relative in Node. When I run the function with 'a/file.js' and 'a/file.css', it returns '../file.css' instead of './file.css'. This is causing confusion f ...

Using axios to pass FormData as a parameter in a post request

I am facing a challenge while trying to upload employee information through a form that requires strings and a picture. I am utilizing axios and node.js for this task, but upon hitting the submit button, I encounter an error (500): message Employees v ...

Challenges arising from CSS position: fixed on iPhone 6+ in landscape orientation running iOS 9

Encountered a strange issue with the fixed header on my website under specific conditions: Using an iPhone 6+ in landscape mode. Using Safari with at least two tabs opened. The page has a fixed position header with html and body set to position: relative ...

What is the most effective method for dividing a string in TypeScript?

Here is the scenario: receiving a string input that looks like Input text: string = "today lunch 200 #hotelname" Output subject: "today lunch" price: 200 tag: #hotelname My initial solution looks like this: text: string = "today lunch 200 #hotelname" ...