我想创建一个计时器,它会在点击通知时开始倒计时。
只要中断长度小于一分钟,我的示例就可以很好地工作。当中断长度超过一分钟时,service worker 停止工作。
索引.html
<!DOCTYPE html>
<html>
<body>
<button onclick="notify();">notify</button>
</body>
<script>
function notify() {
const items = ["First", "End of the break", "Second"];
const options = { data: items };
if ('serviceWorker' in navigator) {
Notification.requestPermission()
.then(() => navigator.serviceWorker.register('sw.js'))
.then(() => navigator.serviceWorker.ready
.then((s) => {
s.showNotification('Click to begin', options);
}))
.catch(e => console.log(e.message));
}
}
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
sw.js
let data = [];
let num = 0;
self.onnotificationclick = (event) => {
event.notification.close();
if (event.notification.data) {
({ data } …Run Code Online (Sandbox Code Playgroud)