-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimer.js
169 lines (141 loc) · 4.85 KB
/
timer.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
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
168
169
var minutes=0;
var seconds=0;
var width = 0;
var timeLeft = 0;
tm=window.setInterval('disp()',1000);
// this function adjusts the text timer underneath the playbar
function disp(){
updateTimer(); // Call updateTimer function to sync with system time
// Format the output by adding 0 if it is single digit
if(seconds<10){var s1='0' + seconds;}
else{var s1=seconds;}
if(minutes<10){var m1='0' + minutes;}
else{var m1=minutes;}
// Display the output
str= m1 +':' + s1 ;
document.getElementById('songTimer').innerHTML=str;
}
function updateTimer() {
var currentDate = new Date();
minutes = currentDate.getMinutes();
seconds = currentDate.getSeconds();
}
function calculateMinutes(){
var todaysDate = new Date();
var pst = todaysDate.toLocaleString('en-US', {
timeZone: 'America/Los_Angeles',
});
todaysDate = new Date(pst);
width = todaysDate.getMinutes() / .6000 // the width will be based on the current time
timeLeft = 3600000 - (width * 36000); // initialize this variable so that update() runs every hour correctly
minutes = todaysDate.getMinutes();
seconds = todaysDate.getSeconds();
}
function updatePlaybar() {
// The progress bar is based from 0 to 100. 100 means the bar is at 1 hour, its full green.
// 0 means the bar is at 0 minutes 0 seconds, its full grey.
// This function increments the variable "width" until it hits 100.
var element = document.getElementById("playbar_inner");
var identity = setInterval(scene, 1000); // will increase the progress bar width every second
function scene() {
var currentDate = new Date();
var currentMinutes = currentDate.getMinutes();
var currentSeconds = currentDate.getSeconds();
// Calculate the width based on the current minutes and seconds
width = (currentMinutes * 60 + currentSeconds) / 3600 * 100;
element.style.width = width + "%";
}
}
updatePlaybar();
calculateMinutes();
setInterval(updatePlaybar, timeLeft); // this will reset the progress bar back to 0/100 every hour
setInterval(calculateMinutes, timeLeft);
updatePlaybar();
calculateMinutes();
setInterval(updatePlaybar, timeLeft); // this will reset the progress bar back to 0/100 every hour
setInterval(calculateMinutes, timeLeft);
/* Possible fix to the timer/progress bar*/
/*
var minutes=0;
var seconds=0;
var width = 0;
var timeLeft = 0;
// Replace setInterval with requestAnimationFrame
function startTimer() {
disp();
requestAnimationFrame(startTimer);
}
requestAnimationFrame(startTimer);
function disp() {
// Format the output by adding 0 if it is single digit
if(seconds<10){var s1='0' + seconds;}
else{var s1=seconds;}
if(minutes<10){var m1='0' + minutes;}
else{var m1=minutes;}
// Display the output
str= m1 +':' + s1 ;
document.getElementById('songTimer').innerHTML=str;
// Calculate the stop watch
if(seconds<59){
seconds=seconds+1;
}else{
seconds=0;
minutes=minutes+1;
if(minutes==60){
minutes=0;
seconds=0;
} // end if minutes == 60
}// end if else seconds < 59
// end of calculation for next display
}
function calculateMinutes() {
var todaysDate = new Date();
var pst = todaysDate.toLocaleString('en-US', {
timeZone: 'America/Los_Angeles',
});
todaysDate = new Date(pst);
width = todaysDate.getMinutes() / .6000 // the width will be based on the current time
timeLeft = 3600000 - (width * 360000); // initialize this variable so that update() runs every hour correctly
minutes = todaysDate.getMinutes();
seconds = todaysDate.getSeconds();
}
function updatePlaybar() {
// The progress bar is based from 0 to 100. 100 means the bar is at 1 hour, its full green.
// 0 means the bar is at 0 minutes 0 seconds, its full grey.
// This function increments the variable "width" until it hits 100.
var element = document.getElementById('playbar_inner');
var identity = setInterval(scene, 1000); // Update every 10 seconds instead of every second
function scene() {
if (width >= 100) {
width = 0;
} else {
width = width + 0.00277777778; // Adjust the increment amount for the 10-second interval
element.style.width = width + '%';
}
}
}
// Replace setInterval with requestAnimationFrame
function startPlaybar() {
updatePlaybar();
requestAnimationFrame(startPlaybar);
}
requestAnimationFrame(startPlaybar);
function resetAndUpdate() {
calculateMinutes();
updatePlaybar();
}
// Add visibilitychange event listener to update the timer when the user switches back to the tab
document.addEventListener('visibilitychange', function() {
if (!document.hidden) {
resetAndUpdate();
}
});
// Replace setInterval with setTimeout and requestAnimationFrame
function startResetAndUpdate() {
resetAndUpdate();
setTimeout(function() {
requestAnimationFrame(startResetAndUpdate);
}, timeLeft);
}
requestAnimationFrame(startResetAndUpdate);
*/