it works almost perfectly but don't uses actual states in generateImage as "isGenerating…
- User
it works almost perfectly but don't uses actual states in generateImage as "isGenerating = true;. isGenerating = false;". it will be nice if you could make a use of these two states inside original "handleProgressBar" to be able to determine when is what to show.
Apologies for the confusion. To incorporate the isGenerating states from the generateImage function into the handleProgressBar function, you can modify the code as follows:
let startTime = null; let previousGenerationTime = null; let isFirstGeneration = true; function generateImage() { isGenerating = true; // Rest of the generateImage logic isGenerating = false; } function handleProgressBar() { const progressBarFilled = document.querySelector(‘.progress-bar-filled’); if (isGenerating) { if (isFirstGeneration) { startTime = Date.now(); isFirstGeneration = false; progressBarFilled.innerText = ‘loading’; } else { const currentTime = Date.now(); const secondsPassed = Math.floor((currentTime - previousGenerationTime) / 1000); progressBarFilled.innerText = done: ${secondsPassed} sec; } previousGenerationTime = Date.now(); } else { isFirstGeneration = true; progressBarFilled.innerText = ‘’; } } // Call the handleProgressBar function periodically setInterval(handleProgressBar, 1000);
In this updated code, the isGenerating flag is set to true at the start of the generateImage function and then set to false at the end. The handleProgressBar function checks the value of isGenerating to determine whether it should display the loading state or calculate the elapsed time. Additionally, a previousGenerationTime variable is introduced to keep track of the previous generation time for calculating the elapsed time accurately. It is set to the current time when the generation starts.
I don't know what you changed but my code already contains an async function generateImage with both of these states inside. if you could reuse them inside actual "function handleProgressBar()" that could be cool.
Apologies for the confusion. To utilize the isGenerating states already present in the generateImage function within the handleProgressBar function, you can modify the code as follows:
let startTime = null; let previousGenerationTime = null; let isFirstGeneration = true; function generateImage() { isGenerating = true; // Rest of the generateImage logic isGenerating = false; } function handleProgressBar() { const progressBarFilled = document.querySelector(‘.progress-bar-filled’); if (isGenerating || isFirstGeneration) { if (isFirstGeneration) { startTime = Date.now(); isFirstGeneration = false; progressBarFilled.innerText = ‘loading’; } else { const currentTime = Date.now(); const secondsPassed = Math.floor((currentTime - previousGenerationTime) / 1000); progressBarFilled.innerText = done: ${secondsPassed} sec; } previousGenerationTime = Date.now(); } else { progressBarFilled.innerText = ‘’; } } // Call the handleProgressBar function periodically setInterval(handleProgressBar, 1000);
In this updated code, the isGenerating flag is checked within the handleProgressBar function. When isGenerating is true or it is the first generation, the progress bar displays ‘loading’ or calculates the elapsed time, respectively. The previousGenerationTime variable is set to the current time when the generation starts or at the beginning of the program.
- Assistant
Apologies for the confusion in my previous response. If you want the handleProgressBar function to be called periodically based on the generateImage processing without actually triggering the generation itself, you can modify the code as follows:
let startTime = null; let previousGenerationTime = null; let isFirstGeneration = true; function generateImage() { // Implement your generateImage logic here } function handleProgressBar() { const progressBarFilled = document.querySelector('.progress-bar-filled'); if (isGenerating || isFirstGeneration) { if (isFirstGeneration) { startTime = Date.now(); isFirstGeneration = false; progressBarFilled.innerText = 'loading'; } else { const currentTime = Date.now(); const secondsPassed = Math.floor((currentTime - previousGenerationTime) / 1000); progressBarFilled.innerText = `done: ${secondsPassed} sec`; } previousGenerationTime = Date.now(); } else { progressBarFilled.innerText = ''; } } // Call the generateImage function and handleProgressBar function periodically setInterval(() => { generateImage(); handleProgressBar(); }, 1000);