To create an animated progress bar in Bootstrap 5 that runs independently from your HTTP request, you can use JavaScript to simulate the progress bar's behavior. Here's a step-by-step guide on how to achieve this:
- First, include the necessary Bootstrap CSS and JS files in your HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Progress Bar Example</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Your content goes here -->
<!-- Bootstrap JS and Popper.js -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
</body>
</html>
- Create a progress bar container with a
progress element inside it:
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
- Add a
span element to display the informal step messages:
<span id="step-message"></span>
- Write JavaScript code to simulate the progress bar and display the informal step messages:
<script>
const progressBar = document.querySelector('.progress-bar');
const stepMessage = document.getElementById('step-message');
const steps = [
{ time: 10, message: 'Warming up servers' },
{ time: 15, message: 'Establishing secure connection' },
{ time: 20, message: 'Preparing the input' },
{ time: 25, message: 'Running Prediction Model' },
{ time: 30, message: 'Completing the request' },
];
let currentStep = 0;
let startTime = Date.now();
function updateProgress() {
const elapsedTime = Date.now() - startTime;
const progress = Math.min((elapsedTime / 1800000), 1); // 3 minutes = 1800000 milliseconds
progressBar.style.width = `${progress * 100}%`;
if (progressBar.style.width === '100%') {
clearInterval(interval);
stepMessage.textContent = 'Request completed';
}
}
function displayStepMessage() {
const step = steps[currentStep];
stepMessage.textContent = step.message;
currentStep++;
if (currentStep === steps.length) {
currentStep = 0;
}
}
const interval = setInterval(updateProgress, 1000);
displayStepMessage();
</script>
This code will create an animated progress bar that runs for exactly 3 minutes and displays informal step messages during the process. The progress bar will disappear when the request is completed.