Ron Au commited on
Commit
dcb7f7f
·
1 Parent(s): ab191ec

fix(ui): Prevent triggering multiple inferences at once

Browse files
Files changed (1) hide show
  1. static/index.js +11 -4
static/index.js CHANGED
@@ -172,7 +172,7 @@ const durationTimer = () => {
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
 
@@ -230,12 +230,20 @@ const cardRotationInitiator = (renderSection) => {
230
  };
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);
@@ -244,16 +252,15 @@ generateButton.addEventListener('click', async () => {
244
  const cleanupTimer = timer(durationSeconds).cleanup;
245
 
246
  const completedTask = await longPollTask(task);
247
-
248
  cleanupTimer();
249
 
250
  renderSection.innerHTML = cardHTML(details);
251
-
252
  const picture = document.querySelector('img.picture');
253
  picture.src = completedTask.value;
254
-
255
  initialiseCardRotation();
256
  } catch (err) {
 
257
  console.error(err);
258
  }
259
  });
 
172
  secondsElement.textContent = duration.toFixed(1);
173
  }, 100);
174
 
175
+ const updateDuration = () => (duration = Number(((performance.now() - startTime) / 1_000).toFixed(1)));
176
 
177
  window.addEventListener('focus', updateDuration);
178
 
 
230
  };
231
  };
232
 
233
+ let generating = false;
234
+
235
  generateButton.addEventListener('click', async () => {
236
+ if (generating) {
237
+ return;
238
+ }
239
+
240
  const renderSection = document.querySelector('section.render');
241
  const durationSeconds = document.querySelector('.duration > .seconds');
242
  const initialiseCardRotation = cardRotationInitiator(renderSection);
243
 
244
  try {
245
+ generating = true;
246
+
247
  const details = await generateDetails();
248
  const task = await createTask(details.energy_type);
249
  queueTask(task.task_id);
 
252
  const cleanupTimer = timer(durationSeconds).cleanup;
253
 
254
  const completedTask = await longPollTask(task);
255
+ generating = false;
256
  cleanupTimer();
257
 
258
  renderSection.innerHTML = cardHTML(details);
 
259
  const picture = document.querySelector('img.picture');
260
  picture.src = completedTask.value;
 
261
  initialiseCardRotation();
262
  } catch (err) {
263
+ generating = false;
264
  console.error(err);
265
  }
266
  });