-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
167 lines (158 loc) · 3.99 KB
/
app.js
File metadata and controls
167 lines (158 loc) · 3.99 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const url="https://www.random.org/integers/"; //HTTP REQUEST
var opts = { //GUAGE CONFIGURATION
angle: -0.2,
lineWidth: 0.2,
radiusScale: 1,
pointer: {
length: 0.6,
strokeWidth: 0.035,
color: '#000000'
},
renderTicks: {
divisions: 10,
divWidth: 1.2,
divLength: 0.7,
subDivisions: 3,
subLength: 0.5,
subWidth: 0.6,
},
staticLabels: {
font: "17px sans-serif",
labels: [0,30,60,90,120,150,180,210,240,270,300],
color: "#000000",
},
staticZones: [
{strokeStyle: "#034a03", min: 1, max: 200},
{strokeStyle: "#b50000", min: 200, max: 300}
],
limitMax: 'false',
percentColors: [[0.0, "#a9d70b" ], [0.50, "#f9c802"], [1.0, "#ff0000"]],
strokeColor: '#E0E0E0',
generateGradient: true
};
var mousedownstop=0;
var down=0;
var gaugestop=0;
var startflag=0;
var stopped=1;
var control=0;
var a=0;
var b=30;
var target = document.getElementById('gauge');
var gauge = new Gauge(target).setOptions(opts);
gauge.maxValue = 300;
gauge.animationSpeed = 80;
gauge.set(0);
document.querySelector(".start").addEventListener("click",()=>{ //TRIGGERING THE keepcalling FUNCTION ON START BUTTON
mousedownstop=0;
gaugestop=0;
stopped=0;
startflag=1;
control=0;
keepcalling();
});
document.querySelector(".stop").addEventListener("click",()=>{ //SETTING THE VARIABLES TO THE DESIRED VALUE ON STOP BUTTON
mousedownstop=1;
gaugestop=1;
stopped=1;
startflag=0;
stopflag=1;
a=0;
b=30;
});
document.querySelector(".brake").addEventListener("pointerdown",()=>{ //TRIGGERING THE applybrakes FUNCTION ON BRAKE mousedown BUTTON
if(mousedownstop!=1){
down=1;
stopped=0;
applybrakes()
}
})
document.querySelector(".brake").addEventListener("pointerup",()=>{ //TRIGGERING THE keepcalling FUNCTION ON BRAKE mouseup BUTTON
down=0;
if(gaugestop==0){
stopped=1;
startflag=1;
control=0;
keepcalling();}
})
function applybrakes(){
if(stopped==0){
startflag=0;
control=1;
fetch(`${url}?num=1&min=${a}&max=${b}&col=1&base=10&format=plain&rnd=new`)
.then(gauge =>{
return gauge.json();
}).then(data=>{
gauge.set(data);
document.querySelector(".speed").textContent=data+" KM/H";
if(a<=10){
document.querySelector(".speed").textContent="0 KM/H";
gauge.set(0);
return;
}
else{
b=a;
a=b-30;
}
applybrakes();
})
.catch(data=>{
document.querySelector(".speed").textContent=data+" KM/H";
if(a==30){
a=0;
b=0;
}
else{
b=a;
a=b-30;
}
applybrakes();
})
}
}
function keepcalling(){
if(down==0){
fetch(`${url}?num=1&min=${a}&max=${b}&col=1&base=10&format=plain&rnd=new`)
.then(gauge =>{
return gauge.json();
}).then(data=>{
gauge.set(data);
document.querySelector(".speed").textContent=data+" KM/H";
if(a==180){
a=180;
b=210;
}
else{
a=b;
b=a+30;
}
if(startflag==1){
keepcalling();
}
else{
if(control!=1){
gauge.set(0);
document.querySelector(".speed").textContent="0 KM/H";
}
}
})
.catch(data=>{
document.querySelector(".speed").textContent=data+" KM/H";
if(a==180){
a=180;
b=210;
}
else{
a=b;
b=a+30;
}
if(startflag==1){
keepcalling();
}
else{
gauge.set(0);
document.querySelector(".speed").textContent="0 KM/H";
}
})
}
}