When I submit the form the submit button disable for 10 seconds and then submit the form. But I want first submit the form and then the button disable for 10 second. or if any other way to do this please suggest..
I only want to change the button color or change the button text after the form was submit and this thing need to 10 second to active like color or text change the button after submit the form
function getRemainingTimer(isNew) {
var now = new Date();
var timer = 5000;
var remainingTimer = 0;
var startTime = localStorage.getItem("startTime");
if (startTime === null) {
if (!isNew) return 0;
localStorage.setItem("startTime", now.getTime());
remainingTimer = timer;
} else {
remainingTimer = timer + parseInt(startTime) - now.getTime();
}
return remainingTimer;
}
function up(isNew) {
var remainingTimer = getRemainingTimer(isNew);
console.log(remainingTimer);
if (remainingTimer > 0) {
$("#upside").attr("disabled", true);
$("#downside").attr("disabled", true);
var timeout = setTimeout(function () {
$("#upside").removeAttr("disabled");
$("#downside").removeAttr("disabled");
window.location = "";
localStorage.removeItem("startTime");
}, remainingTimer);
} else {
localStorage.removeItem("startTime");
}
}
up(false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="" action="#">
<button id="upside" onclick="up(true)" type="submit" class="">Up</button>
</form>
3
Answers
Since you are already using javascript to handle the button click, you achieve this, by dynamically submitting the form with the button.
Give the form an ID, and change type="submit" to type="button" like so:
Then, submit the form dynamically before disabling button:
Mind that since submitting a form refreshes the page, it’s better to run the .submit() function only after you set all your needed localstorage items.
To achieve your desired behavior, you can modify the code slightly. Instead of disabling the button for 10 seconds before submitting the form, you can submit the form first and then change the button’s color or text while waiting for 10 seconds. Here’s how you can do it:
Remove the onclick attribute from the button in the HTML form.
Add an event listener to the form submission using jQuery.
In the event listener, prevent the default form submission behavior.
After preventing the default behavior, change the button’s color or text.
Use a timeout to wait for 10 seconds, then revert the button’s color or text back to the original state.
Here’s the updated code:
This code will change the button’s text to "Submitting…" when the form is submitted, wait for 10 seconds, and then revert the button’s text back to the original state. You can customize this further to suit your design and behavior preferences.
I think I was able to give you the answer you wanted
can be this way in pure JS