-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
46 lines (36 loc) · 1.21 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const CACHE_NAME = "proto_overwatchoffline2";
const FILE_NAME = './';
// DOM elements
const status = document.getElementById("js-status");
const cacheButton = document.getElementById("js-cache-btn");
const removeCacheButton = document.getElementById("js-remove-cache-btn");
// Hook up event listeners
cacheButton.addEventListener('click', addToCache);
removeCacheButton.addEventListener('click', removeFromCache);
(function main(){
updateStatus();
})();
function updateStatus() {
isCached().then(value => {
status.innerText = '' + value;
});
}
function isCached() {
return window.caches.open(CACHE_NAME)
.then(cache => cache.match(FILE_NAME))
.then(Boolean);
}
function addToCache() {
window.caches.open(CACHE_NAME)
.then(cache => cache.add(FILE_NAME))
.then(() => console.log('cached file musik'))
.catch(e => console.error('gagal cache file musik', e))
.finally(updateStatus);
}
function removeFromCache() {
window.caches.open(CACHE_NAME)
.then(cache => cache.delete(FILE_NAME))
.then(() => console.log('hapus cache file musik'))
.catch(e => console.error('gagal menghapus cache file musik', e))
.finally(updateStatus);
}