I have a button in my HTML file which calls a post route when it's clicked and submits a form. The button click also calls a function in my other javascript file that activates a 'sweetalert' alert box.
However, the problem that I have now is that the user does not get enough time to read the alert before the post request executes res.redirect("/").
I was hoping for any one of the following solutions:
1) Call the alert after the page is redirected (I read somewhere that you cannot control the next page with node.js so this may not be possible).
2) Wait a few seconds before res.redirect("/") is called.
3) Any other appropriate solution.
HTML file that contains the button:
<button formaction="/removetask" type="submit" id="top" class="btn btn-primary btn-block" onclick="success()"> Complete </button>
client-side javascript file:
function success(){
...
swal("Congratulations!", "Goal successfully completed", "success")
}
node.js file:
app.post("/removetask", async function(req, res) {
...
res.redirect("/");
});
link to 'sweetalert' in HTML:
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
EDIT:
I notice that using the regular HTML alert() stops everything and prevents redirection until the user has closed the alert. Is it possible to change the 'sweetalert` alert to behave like this?