-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
111 lines (84 loc) · 2.52 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const time = document.getElementById("time");
const milisec = document.getElementById("milisec");
const quotesurl = 'https://raw.githubusercontent.com/Mayborg121/QuotesFileHostin/refs/heads/main/quotes.json';
let quote = document.getElementById("quote");
let author = document.getElementById("author");
let quotes;
let a;
let timer = null;
let startTime = 0;
let elapsedTime = 0;
let isRunning = false;
function start(){
if(!isRunning){
startTime = Date.now() - elapsedTime;
timer = setInterval(update, 10);
isRunning = true;
}
// Fetch the JSON data
fetch(quotesurl)
.then(response => response.json())
.then(data => {
// Handle the JSON data here
quotes = data[Math.floor(Math.random()*(data.length))];
setquote(quotes);
})
.catch(error => {
console.error('Error fetching JSON:', error);
});
}
function stop(){
if(isRunning){
clearInterval(timer);
elapsedTime = Date.now() - startTime;
isRunning = false;
}
displayquotes(a);
}
function reset(){
clearInterval(timer);
startTime = 0;
elapsedTime = 0;
isRunning = false;
time.textContent = "00:00:00";
milisec.textContent = "00";
}
function update(){
const currentTime = Date.now();
elapsedTime = currentTime - startTime;
let hours = Math.floor(elapsedTime / (1000 * 60 * 60));
let minutes = Math.floor(elapsedTime / (1000 *60) % 60);
let seconds = Math.floor(elapsedTime / 1000 % 60);
let miliseconds = Math.floor(elapsedTime % 1000 / 10);
hours = String(hours).padStart(2,"0");
minutes = String(minutes).padStart(2,"0");
seconds = String(seconds).padStart(2,"0");
miliseconds = String(miliseconds).padStart(2,"0");
time.textContent = `${hours}:${minutes}:${seconds}`;
milisec.textContent = `${miliseconds}`;
}
//Keyboard Bindings
window.onkeydown = function(event) {
if (event.keyCode == 32) {
if(isRunning){
stop();
}
else{
start();
}
};
if (event.keyCode == 8){
reset();
}
if (event.keyCode == 16){
reset();
}
};
function setquote(quote){
a = quote;
}
function displayquotes(a){
console.log(a.Quote);
quote.textContent = String(`" `+a.Quote+` "`);
author.textContent = String(`✨- `+a.Author);
}