function bufferFormInput() {
const form = document.querySelector('form'); // Get the form element
if (!form) return;
const originalSubmit = form.submit; // Store the original submit function
form.addEventListener('submit', (event) => {
event.preventDefault(); // Prevent default form submission
const inputValues = {}; // Store input values
const inputs = form.elements;
for (let i = 0; i < inputs.length; i++) {
const input = inputs[i];
if (input.type !== 'submit' && input.tagName !== 'LABEL') { // Avoid submit buttons and labels
if (input.value) {
inputValues[input.name] = input.value; // Store input value by name
}
}
}
// Simulate form submission after a delay
setTimeout(() => {
// Process the buffered data
processForm(inputValues);
}, 500); // Adjust the delay as needed (milliseconds)
});
form.submit = (event) => {
event.preventDefault(); // Prevent immediate submission
};
}
function processForm(data) {
// Implement your logic to handle the buffered data here
console.log("Buffered data:", data);
//Example:
// const formData = new FormData();
// for (const key in data) {
// formData.append(key, data[key]);
// }
// fetch('/submit', { method: 'POST', body: formData })
// .then(response => response.json())
// .then(data => console.log(data));
}
Add your comment