Spaces:
Sleeping
Sleeping
Ron Au
commited on
Commit
·
372755f
1
Parent(s):
2458d58
fix(ui): Update duration if inference finishes with window out of focus
Browse files- static/index.js +30 -16
static/index.js
CHANGED
@@ -161,6 +161,32 @@ const longPollTask = async (task, interval = 5_000, max) => {
|
|
161 |
|
162 |
const generateButton = document.querySelector('button.generate');
|
163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
const rotateCard = () => {
|
165 |
const RANGE = 0.1;
|
166 |
const INTERVAL = 13; // ~75 per second
|
@@ -205,33 +231,21 @@ const cardRotationInitiator = (renderSection) => {
|
|
205 |
};
|
206 |
|
207 |
generateButton.addEventListener('click', async () => {
|
208 |
-
const details = await generateDetails();
|
209 |
-
|
210 |
const renderSection = document.querySelector('section.render');
|
211 |
const durationSeconds = document.querySelector('.duration > .seconds');
|
212 |
const initialiseCardRotation = cardRotationInitiator(renderSection);
|
213 |
-
let duration = 0.0;
|
214 |
|
215 |
try {
|
|
|
216 |
const task = await createTask(details.energy_type);
|
217 |
-
|
218 |
queueTask(task.task_id);
|
219 |
|
220 |
-
const
|
221 |
-
|
222 |
-
const incrementSeconds = setInterval(() => {
|
223 |
-
console.log(typeof duration);
|
224 |
-
duration += 0.1;
|
225 |
-
durationSeconds.textContent = duration.toFixed(1);
|
226 |
-
}, 100);
|
227 |
-
|
228 |
-
window.addEventListener('focus', () => {
|
229 |
-
duration = Number(((performance.now() - startTime) / 1000).toFixed(1));
|
230 |
-
});
|
231 |
|
232 |
const completedTask = await longPollTask(task);
|
233 |
|
234 |
-
|
235 |
|
236 |
renderSection.innerHTML = cardHTML(details);
|
237 |
|
|
|
161 |
|
162 |
const generateButton = document.querySelector('button.generate');
|
163 |
|
164 |
+
const durationTimer = () => {
|
165 |
+
let duration = 0.0;
|
166 |
+
|
167 |
+
return (secondsElement) => {
|
168 |
+
const startTime = performance.now();
|
169 |
+
|
170 |
+
const incrementSeconds = setInterval(() => {
|
171 |
+
duration += 0.1;
|
172 |
+
secondsElement.textContent = duration.toFixed(1);
|
173 |
+
}, 100);
|
174 |
+
|
175 |
+
const updateDuration = () => (duration = Number(((performance.now() - startTime) / 1000).toFixed(1)));
|
176 |
+
|
177 |
+
window.addEventListener('focus', updateDuration);
|
178 |
+
|
179 |
+
return {
|
180 |
+
cleanup: () => {
|
181 |
+
updateDuration();
|
182 |
+
clearInterval(incrementSeconds);
|
183 |
+
window.removeEventListener('focus', updateDuration);
|
184 |
+
secondsElement.textContent = duration.toFixed(1);
|
185 |
+
},
|
186 |
+
};
|
187 |
+
};
|
188 |
+
};
|
189 |
+
|
190 |
const rotateCard = () => {
|
191 |
const RANGE = 0.1;
|
192 |
const INTERVAL = 13; // ~75 per second
|
|
|
231 |
};
|
232 |
|
233 |
generateButton.addEventListener('click', async () => {
|
|
|
|
|
234 |
const renderSection = document.querySelector('section.render');
|
235 |
const durationSeconds = document.querySelector('.duration > .seconds');
|
236 |
const initialiseCardRotation = cardRotationInitiator(renderSection);
|
|
|
237 |
|
238 |
try {
|
239 |
+
const details = await generateDetails();
|
240 |
const task = await createTask(details.energy_type);
|
|
|
241 |
queueTask(task.task_id);
|
242 |
|
243 |
+
const timer = durationTimer();
|
244 |
+
const cleanupTimer = timer(durationSeconds).cleanup;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
|
246 |
const completedTask = await longPollTask(task);
|
247 |
|
248 |
+
cleanupTimer();
|
249 |
|
250 |
renderSection.innerHTML = cardHTML(details);
|
251 |
|