-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.js
More file actions
36 lines (32 loc) · 1.33 KB
/
Copy pathcounter.js
File metadata and controls
36 lines (32 loc) · 1.33 KB
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
// is there already a value for c in local storage
if (!localStorage.getItem('c')) {
localStorage.setItem('c', 0);
}
function Counter() {
let c = localStorage.getItem('c');
c++;
// alert(`Counter = ${c}`);
document.getElementById("p").innerText = c;
// if (c % 10 === 0) {
// document.querySelector('h1').innerHTML = `Goodbye! you have clicked ${c} times`;
// }
// else {
// document.querySelector('h1').innerHTML = "Welcome!";
// }
// // another way of doing the same thing
// if (document.querySelector('h2').innerHTML === "Hello!") {
// document.querySelector('h2').innerHTML = "Goodbye!";
// }
// else {
// document.querySelector('h2').innerHTML = "Hello!";
// }
// update the c value with the new value
localStorage.setItem('c', c);
}
// adding an event listener
document.addEventListener('DOMContentLoaded', function () { // this will run only when the content is fully loaded.
document.querySelector('#p').innerHTML = localStorage.getItem('c');
document.querySelectorAll('button')[1].onclick = Counter;
// document.querySelectorAll('button')[1].addEventListener('click', 'Counter'); // we can do the same using to addEventListener also.
//setInterval(Counter, 1000); // after every 1sec or 1000 ms the Counter function will run.
});