Javascript 每次刷新后仅显示一次通知
我想做的只是在通知允许后显示通知 我现在看到的是,每次刷新页面后,它都会显示通知 这是我的javascriptJavascript 每次刷新后仅显示一次通知,javascript,notifications,Javascript,Notifications,我想做的只是在通知允许后显示通知 我现在看到的是,每次刷新页面后,它都会显示通知 这是我的javascript function notifyMe() { function AutoRefresh( t ) { setTimeout("location.reload(true);", t); } // Let's check if the browser supports notifications if
function notifyMe() {
function AutoRefresh( t ) {
setTimeout("location.reload(true);", t);
}
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
}
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
}
}
});
}
}
我应该怎么做才能只发出一次通知,即使是在刷新之后。您必须跟踪用户是否已经看到通知,如果已经看到,则跳过
notifyMe
功能的其余部分。例如,您可以将信息存储在localStorage
中
function notifyMe() {
function AutoRefresh(t) {
// passing strings to setTimeout makes me shiver
// too many years of avoiding eval and stuff i guess :)
setTimeout(function () { location.reload(true); }, t);
}
if (localStorage.getItem('userHasSeenNotification')) {
return; // early return, exits the function
}
if (!('Notification' in window)) {
alert('Notifications ... ');
return; // early return, lets us get rid of the "else"
}
// Let's check whether notification permissions have already been granted
if (Notification.permission === "granted") {
// save seen state
localStorage.setItem('userHasSeenNotification', 1);
// If it's okay let's create a notification
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
};
return; // ... again ...
}
// Otherwise, we need to ask the user for permission
if (Notification.permission !== "denied") {
// save seen state
localStorage.setItem('userHasSeenNotification', 1);
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
};
}
});
}
}
}
提示:您可能希望将最后一个
localStorage.setItem
调用移动到传递给的回调中。然后(函数(权限){…})
您必须跟踪用户是否已看到通知,如果是,则跳过notifyMe
函数的其余部分。例如,您可以将信息存储在localStorage
中
function notifyMe() {
function AutoRefresh(t) {
// passing strings to setTimeout makes me shiver
// too many years of avoiding eval and stuff i guess :)
setTimeout(function () { location.reload(true); }, t);
}
if (localStorage.getItem('userHasSeenNotification')) {
return; // early return, exits the function
}
if (!('Notification' in window)) {
alert('Notifications ... ');
return; // early return, lets us get rid of the "else"
}
// Let's check whether notification permissions have already been granted
if (Notification.permission === "granted") {
// save seen state
localStorage.setItem('userHasSeenNotification', 1);
// If it's okay let's create a notification
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
};
return; // ... again ...
}
// Otherwise, we need to ask the user for permission
if (Notification.permission !== "denied") {
// save seen state
localStorage.setItem('userHasSeenNotification', 1);
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
};
}
});
}
}
}
提示:您可能希望将最后一个
localStorage.setItem
调用移动到传递给的回调中。然后(函数(权限){…}
,但我仅将其用于来宾,因此我没有关于谁看到该通知的用户信息。用户“登录”到某个对象并不是必需的localStorage
是一个直接构建到浏览器中的存储,它允许在客户端存储信息。你可以在MDN上读到更多关于它的内容:这对我不起作用,还有什么别的吗?我把所有的密码都写上了。它没有显示任何悲伤什么不起作用?您在控制台中收到消息了吗?您是否尝试了调试器语句来确定它在哪一点失败?以及:如果您在测试时拒绝了通知,您是否在浏览器设置中重置了权限?另外,编辑通知的初始值。权限既不是“授予”也不是“拒绝”
,它是“默认”
,您没有任何if
-声明。但我只为客人使用它,所以我没有关于谁看到通知的用户信息。用户不必“登录”他说localStorage
是一个直接构建到浏览器中的存储,它允许在客户端存储信息。你可以在MDN上读到更多关于它的内容:这对我不起作用,还有什么别的吗?我把所有的密码都写上了。它没有显示任何悲伤什么不起作用?您在控制台中收到消息了吗?您是否尝试了调试器语句来确定它在哪一点失败?以及:如果您在测试时拒绝了通知,您是否在浏览器设置中重置了权限?同时编辑,通知.权限的初始值既不是授予的
也不是“拒绝的”
,它是“默认的”
,您没有任何if
-语句。