Here's a step-by-step guide to help you achieve the desired animated progress bar with estimated time left and informal step indicators using Bootstrap 5, all running independently of your HTTP request for exactly 3 minutes:
HTML Structure
First, include Bootstrap 5 in your project if you haven't already. Then, add the following HTML structure to your page where you want the progress bar to appear:
<!-- Container for the progress bar and related information -->
<div id="progressBarContainer" style="display:none;">
<div class="progress">
<!-- The progress bar element, animated and striped for visual effect -->
<div id="progressBar" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<!-- Displays the estimated time left for the operation -->
<p id="estimatedTimeLeft"></p>
<!-- Shows the current step or process being performed -->
<p id="currentStep"></p>
</div>
JavaScript
Now, add the following JavaScript code to your page. This script will control the progress bar, estimated time left, and the current step indicators.
document.addEventListener('DOMContentLoaded', (event) => {
// Show the progress bar container once the DOM is loaded
document.getElementById("progressBarContainer").style.display = "block";
const progressBar = document.getElementById("progressBar");
const estimatedTimeLeftElement = document.getElementById("estimatedTimeLeft");
const currentStepElement = document.getElementById("currentStep");
// Define steps with their durations in seconds
const steps = [
{ name: "Warming up servers", duration: 10 },
{ name: "Establishing secure connection", duration: 15 },
{ name: "Preparing the input", duration: 30 },
{ name: "Running Prediction Model", duration: 120 }, // This will fill the remaining time
];
const totalTimeInSeconds = 180; // Total duration: 3 minutes in seconds
let currentTimeInSeconds = 0; // Tracks the elapsed time
let currentStepIndex = 0; // Keeps track of the current step
let timeElapsedInCurrentStep = 0; // Tracks time spent in the current step
// Update the progress bar every second
const intervalId = setInterval(() => {
currentTimeInSeconds++; // Increment elapsed time
timeElapsedInCurrentStep++; // Increment time in the current step
// Update Progress Bar
const percentage = (currentTimeInSeconds / totalTimeInSeconds) * 100;
progressBar.ariaValueNow = percentage;
progressBar.style.width = `${percentage}%`;
// Update Estimated Time Left
const estimatedTimeLeft = totalTimeInSeconds - currentTimeInSeconds;
const minutes = Math.floor(estimatedTimeLeft / 60);
const seconds = estimatedTimeLeft % 60;
estimatedTimeLeftElement.textContent = `Estimated Time Left: ${minutes}m ${seconds}s`;
// Update Current Step
if (timeElapsedInCurrentStep > steps[currentStepIndex].duration) {
currentStepIndex++; // Move to the next step if time's up
timeElapsedInCurrentStep = 0; // Reset timer for the new step
}
// Ensure we don't go out of bounds
if (currentStepIndex < steps.length) {
currentStepElement.textContent = steps[currentStepIndex].name;
}
// Clear interval after 3 minutes
if (currentTimeInSeconds >= totalTimeInSeconds) {
clearInterval(intervalId);
// Hide the progress bar container after completion
setTimeout(() => {
document.getElementById("progressBarContainer").style.display = "none";
}, 500); // Wait half a second before hiding
}
}, 1000); // Update every 1 second
});
How It Works:
- Initialization: The script waits for the DOM to load, then displays the progress bar container.
- Steps Configuration: An array of steps with their durations is defined. The last step's duration is set to fill the remaining time up to 3 minutes.
- Interval Updates: Every second, the script updates the progress bar's width, the estimated time left, and checks if it's time to move to the next step based on the elapsed time within the current step.
- Completion: After exactly 3 minutes, the interval clears, and the progress bar container hides after a brief delay.
This solution should provide the functionality you described, running independently of your HTTP request and disappearing after exactly 3 minutes.