-
Notifications
You must be signed in to change notification settings - Fork 0
/
storageVisitCounter.js
40 lines (29 loc) · 1.06 KB
/
storageVisitCounter.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
window.addEventListener("load", (() => {
function showNotice(...params) {
alert('You have visited the page more than ' + params + ' times!' );
}
function storageVisitCounter(count, collback) {
const cntSess = window.sessionStorage.getItem('cntSess');
let cntLocal = window.localStorage.getItem('cntLocal');
console.log({
'cntLocal': [cntLocal, typeof cntLocal],
'cntSess': [cntSess, typeof cntSess]
});
if (cntLocal < count || cntLocal===null) {
if (cntSess===null) {
if(cntLocal===null) {
cntLocal = 1;
} else {
cntLocal = (parseInt(cntLocal));
cntLocal++;
}
console.log('Set cntLocal:' + cntLocal);
window.localStorage.setItem('cntLocal', cntLocal);
window.sessionStorage.setItem('cntSess', 'Y');
}
} else {
collback(cntLocal);
}
}
storageVisitCounter(3, showNotice);
}));