Search code examples
javascriptjquerynoty

With multiple notifications on screen, closing a notification closes the last notification


I am using NOTY (notification library) version 3.0.0 to show notification popups from JavaScript.

While having multiple notifications on the screen, when I click the close button, it always closes the last notification placed on screen. I know this is because the variable n is always overwritten by the last notification object in the sample code. But, I didn't find any clue in the documentation about how to handle notification close in case of multiple notifications.

Should I create an array of notification objects and then calling the .close() function like this n[i].close(), or do we have any better way to achieve this? Help please.

Sample code:

var n = new Noty({
    type: 'success',
    layout: 'bottomRight',
    theme: 'relax',
    text: "<b>Testing 2<br/><br/> ",
    timeout: false,
    closeWith: ['button'],
    animation: {
        open: 'noty_effects_open',
        close: 'noty_effects_close'
    },
    buttons: [
        Noty.button('CLOSE', 'btn btn-error', function () {
            console.log('button 2 clicked');
            n.close();
        })
      ],
}).show();

Solution

  • I think you can use the queue functionality where in you can push multiple objects to a queue variable and can manipulate those using below examples

    Noty.closeAll(); // Closes all notifications
    Noty.closeAll('myCustomQueueName'); // Closes all notifications with queue named 'myCustomQueueName'
    
    Noty.setMaxVisible(10); // Sets the maxVisible notification count for global queue;
    Noty.setMaxVisible(10, 'myCustomQueueName'); // Sets the maxVisible notification count for 'myCustomQueueName' queue;