I am installing a search bar, and I want it to appear when the form is submitted, and complete/dissapear when the response is returned. Any idea on how to do so? I am working with the bootstrap progress bar
<div class="progress" style="height: 5px;">
<div class="progress-bar" style="color:#EB7051;" style="background-color: #ffffff" role="progressbar" style="width: 25%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
I know this is HTML, but would I have to do anything with my fetch or POST request? That relevant code is below:
let progress = 0;
const maxTime = 5000; // 5 seconds
let interval = null;
// onclick or the event that start the call
interval = setInterval(() => {
progress = progress >= 100 ? 100 : progress + 1
document.getElementById('myprogress').style.width = `${progress}%`
// end interval and wait at 100%
if(progress == 100) clearInterval(interval);
}, maxTime/100)
document.getElementById('loadingcontainer').style.display = "none"
function searchIt() {
let form = document.querySelector('form')
console.log(form)
form.addEventListener('submit', async(e) => {
e.preventDefault()
let urlIN = form.url.value
let url = encodeURIComponent(urlIN)
console.log(url)
try {
const data = await fetch('/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: url
})
}).then(res => {
document.open()
res.text().then(function(text) {
document.write(text)
// Hide the progressbar, stop the timer and reset progress
clearInterval(interval);
progress = 0;
document.getElementById('myprogress').style.width = "0%"
document.getElementById('loadingcontainer').style.display = "";
});
})
} catch (err) {
console.error(err)
}
})
}
Progress-bar:
.progress-bar {
background-color: rgb(255, 255, 255);
color: rgb(197, 90, 28);
-webkit-box-shadow: none;
box-shadow: none;
}
</style>
<div id="loadingcontainer">
<div class="progress" style="height: 5px;">
<div id="myprogress" class="progress-bar" style="color:#EB7051;" style="background-color: #ffffff" role="progressbar" style="width: 25%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
question from:
https://stackoverflow.com/questions/65877889/progress-bar-render-on-form-submit-complete-on-data-response 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…