How do I dismiss a notification?

I have a notification that displays for a set amount of milliseconds. How can I dismiss the notification before the timeout dismisses it?

To clear a notification you can simply bring up another notification with a small timeout. For example:

Here I am setting a notification to display for 30 seconds in a function I know is heavy on processing.

notification.info("Loading Items... ", {timeout: 30 * 1000 });

Now, in this case the task might not run 30 seconds, but only 5 seconds, we don't want that message to display for 30 seconds at all.

I then re-display the same notification, but with the timeout set to less than a second:

notification.info("Loading Items... ", {timeout: 500 });

This will cause the previous 30 second message to be dismissed and this one shown in its place. Since it's only showing for 500ms, it will be brief and disappear. Why not set the timeout to 1 or zero, well, if a user is expecting a message to pop up, we still want to ensure the user sees the message rather than never see the message if the function runs too fast in special cases.

The reason why this works is that we are only allowed to show one notification to the user at a time, so leveraging this rule, we can simply override the current notification.

6 Likes