@@ -116,30 +116,45 @@ <h1>Jury Tool</h1>
116
116
} ) ;
117
117
118
118
function updateEventData ( ) {
119
- // Update text to "Loading..." once fetching starts
120
119
events . forEach ( ( event ) => {
121
- document . getElementById ( event + '-data' ) . textContent = 'Loading...' ;
120
+ document . getElementById ( event + '-data' ) . textContent =
121
+ 'Waiting for next action...' ;
122
122
document . getElementById ( event + '-vote' ) . textContent = 'Loading...' ;
123
+ pollEventData ( event ) ;
124
+ pollEventVotes ( event ) ;
123
125
} ) ;
126
+ }
127
+
128
+ function pollEventData ( event ) {
129
+ fetch ( `${ backendUrl } /events/${ event } /subscribe` )
130
+ . then ( ( response ) => response . json ( ) )
131
+ . then ( ( data ) => {
132
+ document . getElementById ( event + '-data' ) . textContent =
133
+ JSON . stringify ( data , null , 2 ) ;
134
+ // Call the function again to keep polling
135
+ pollEventData ( event ) ;
136
+ } )
137
+ . catch ( ( error ) => {
138
+ console . error ( 'Error polling event data:' , error ) ;
139
+ // Retry after a short delay in case of an error
140
+ setTimeout ( ( ) => pollEventData ( event ) , 1000 ) ;
141
+ } ) ;
142
+ }
124
143
125
- // Update data for each event, every second
126
- setInterval ( ( ) => {
127
- events . forEach ( ( event ) => {
128
- fetch ( `${ backendUrl } /events/${ event } /subscribe?wait=false` )
129
- . then ( ( response ) => response . json ( ) )
130
- . then ( ( data ) => {
131
- document . getElementById ( event + '-data' ) . textContent =
132
- JSON . stringify ( data , null , 2 ) ;
133
- } ) ;
134
-
135
- fetch ( `${ backendUrl } /events/${ event } /vote/results` )
136
- . then ( ( response ) => response . json ( ) )
137
- . then ( ( data ) => {
138
- document . getElementById ( event + '-vote' ) . textContent =
139
- JSON . stringify ( data , null , 2 ) ;
140
- } ) ;
144
+ function pollEventVotes ( event ) {
145
+ fetch ( `${ backendUrl } /events/${ event } /vote/results` )
146
+ . then ( ( response ) => response . json ( ) )
147
+ . then ( ( data ) => {
148
+ document . getElementById ( event + '-vote' ) . textContent =
149
+ JSON . stringify ( data , null , 2 ) ;
150
+ // Call the function again to keep polling
151
+ pollEventVotes ( event ) ;
152
+ } )
153
+ . catch ( ( error ) => {
154
+ console . error ( 'Error polling event votes:' , error ) ;
155
+ // Retry after a short delay in case of an error
156
+ setTimeout ( ( ) => pollEventVotes ( event ) , 1000 ) ;
141
157
} ) ;
142
- } , 1000 ) ;
143
158
}
144
159
</ script >
145
160
</ body >
0 commit comments