I am trying to write JS to wait and redirect after button click. Note this button is also posting message to backend Flask to trigger a function.
My HTML code:
<form action="{{ url_for('start_task1') }}" method="post">
<button type="submit" onclick="pageRedirect();">Submit</button>
</form>
<div id="message"></div>
<script>
function pageRedirect(){
var delay = 6000; // time in milliseconds
// Display message after button click
document.getElementById("message").innerHTML = "Please wait, you are redirecting to the new page.";
setTimeout(function(){
window.location.href = "/flaskendpoint";
},delay);
}
</script>
Expected behavior when I was trying:
The way I thought it should have worked is that after button click, the page displays the "message" and waits for 6 seconds and then it redirects to another view function / endpoint like this --> (/flaskendpoint) in my Flask app.
Problem:
Problem I am getting is that after button click the "message" appears on the page only momentarily and then it vanishes, and the page does not redirect.
There are no errors in console.