Allow HTML5 notification after a delay?

I want to use HTML5 notifications that work great.

The problem is that they never disappear.

How to set the delay after which the HTML5 notification disappears?

+4
source share
2 answers

You can simply call the method .close():

var n = new Notification("Hello");
setTimeout(n.close.bind(n), 2000);

See here at MDN for more details .

+6
source

The notification should have a built-in close button, no?

HTML

<a href="#" onclick="return show()">Notify me!</a>

Js

<script>
        var Notification = window.Notification || window.mozNotification || window.webkitNotification;

        Notification.requestPermission(function (permission) {
            // console.log(permission);
        });

        function show() {
            var instance = new Notification(
                "Hey", {
                    body: "this is a message"
                }
            );

            instance.onclick = function () {
                // Something to do
            };
            instance.onerror = function () {
                // Something to do
            };
            instance.onshow = function () {
                // Something to do
            };
            instance.onclose = function () {
                // Something to do
            };

            return false;
        }
    </script>
+1
source

Source: https://habr.com/ru/post/1624361/


All Articles