这可以使用 setInterval() 吗?
在下面的示例中,警报是每 5 秒...
<script type="text/javascript">
setInterval(function() {
// alert("Message to alert every 5 seconds");
}, 5000);
</script>
我正在尝试在间隔函数中运行 safari 推送通知,也许这不是最佳做法,但目前是一种解决方法
function Notifier() {}
// Returns "true" if this browser supports notifications.
Notifier.prototype.HasSupport = function() {
if (window.webkitNotifications) {
return true;
} else {
return false;
}
}
// Request permission for this page to send notifications. If allowed,
// calls function "cb" with true.
Notifier.prototype.RequestPermission = function(cb) {
window.webkitNotifications.requestPermission(function() {
if (cb) { cb(window.webkitNotifications.checkPermission() == 0); }
});
}
// Popup a notification with icon, title, and body. Returns false if
// permission was not granted.
Notifier.prototype.Notify = function(icon, title, body) {
if (window.webkitNotifications.checkPermission() == 0) {
var popup = window.webkitNotifications.createNotification(
icon, title, body);
popup.show();
return true;
}
return false;
}
$(function() {
var notifier = new Notifier();
if (!notifier.HasSupport()) {
$("#error").show();
return;
}
$("#request-permission").click(function() {
$("#error").hide();
notifier.RequestPermission();
});
$(function() {
if (!notifier.Notify("#", "MESSAGE")) {
$("#error").text('Permission denied. Click "Request Permission" to give this domain access to send notifications to your desktop.');
$("#error").show();
} else {
$("#error").hide();
}
});
});