通过periodicSync尝试过,但不起作用:
TypeError: Cannot read property 'register' of undefined
这篇文档说periodicSync在任何地方都不受支持:
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/periodicSync
有可能实现这一点吗?
self.addEventListener('activate', event => {
while (true) {
//timer 1 min
//POST query
}
});那么,当重新打开浏览器的请求持续时,该怎么办呢?
发布于 2020-01-06 17:26:15
TypeError: Cannot read property 'register' of undefined此错误与periodicSync无关,它与服务工作者的注册有关。
因此,跨浏览器的更系统的方法将使用fetch API https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
去做这件事
self.addEventListener('THE_EVENT_TO_BE_USED', event => {
setInterval(function() {
try {
const url = 'https://randomuser.me/api';
// The data we are going to send in our request
let data = {
name: 'Sara'
}
// The parameters we are gonna pass to the fetch function
let fetchData = {
method: 'POST',
body: data,
headers: new Headers()
}
const registration = await navigator.serviceWorker.ready;
registration.backgroundFetch.fetch(url, fetchData).then(function(data) {
// Handle response you get from the server
}).catch(function(error) {
// If there is any error you will catch them here
});
} catch (err) {
console.error(err);
}
}, 60000);
});https://stackoverflow.com/questions/59608830
复制相似问题