[英]set timeout to notification javascript
I have a website notification When sending the notification, it is sent now.我有一个网站通知发送通知时,现在发送。 I want to send it two hours after pressing the send notification button
我想在按下发送通知按钮两个小时后发送
CODE代码
<button onclick="bell();">send</button>
function bell() {
function notifyMe() {
if (!("Notification" in window)) {
alert("This browser does not support system notifications");
}
else if (Notification.permission === "granted") {
notify();
}
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if (permission === "granted") {
notify();
}
});
}
function notify() {
var notification = new Notification('success now', {
icon: 'blue.svg',
});
setTimeout(notification.close.bind(notification), 3000);
var notification = new Notification('success 2 hours', {
icon: 'blue.svg',
}); notification.onclick = function () {
window.open("INDEX.HTML");
};
}
}
notifyMe();
}
You can use this code:您可以使用此代码:
<!DOCTYPE html>
<html>
<body>
<button onclick="bell()">Show notification</button>
<script type="text/javascript">
const granted = (async () => {
let granted = false;
if (Notification.permission === 'granted') {
granted = true;
} else if (Notification.permission !== 'denied') {
let permission = await Notification.requestPermission();
granted = permission === 'granted' ? true : false;
}
return granted;
})();
const showNotification = (msg) => {
const notification = new Notification('success', {
body: msg,
icon: 'blue.svg'
});
setTimeout(() => {
notification.close();
}, 3 * 1000);
notification.onclick = () => {
window.focus();
}
}
function bell() {
granted && showNotification("Hello World"); // short for if (granted) showNotification(...)
}
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.