I am working on developing a script that triggers the submit button at the end of a form at a specified time on client side. The goal of this test is to simulate a scenario where multiple users submit the same form simultaneously but with different details. Imagine 20 people in a room, all connected to the same network and each with their own device. They’ve all completed the application, and when someone says ‘1, 2, 3, go,’ they all press the submit button at the exact same time.
I created the following script with the help of AI, but the problem is that it only refreshes the page instead of actually triggering the submit button.
let me tell you in advance that I am a beginner.
// Function to schedule a submit request
function scheduleSubmit() {
// Set the specific time to submit
const timeInput = "10:30 PM"; // Change this to your desired time
// Validate the time input format
const timePattern = /^(0?[1-9]|1[0-2]):[0-5][0-9] (AM|PM)$/;
if (!timePattern.test(timeInput)) {
console.error("Invalid time format. Please use HH:MM AM/PM format.");
return;
}
const [time, period] = timeInput.split(' ');
let [hours, minutes] = time.split(':').map(Number);
// Convert hours to 24-hour format
if (period === 'PM' && hours < 12) {
hours += 12;
} else if (period === 'AM' && hours === 12) {
hours = 0;
}
const now = new Date();
const submitTime = new Date();
// Set the submit time to today at the specified hours and minutes
submitTime.setHours(hours, minutes, 0, 0);
// If the specified time is in the past, set it for tomorrow
if (submitTime < now) {
submitTime.setDate(submitTime.getDate() + 1);
}
// Schedule the submit button click
const delay = submitTime - now;
setTimeout(() => {
const submitButton = document.querySelector('.btn.btn-danger.btn-block.mt-3');
if (submitButton) {
submitButton.click();
console.log(`Submit triggered at ${submitTime.toLocaleTimeString()}`);
} else {
console.error("Submit button not found.");
}
}, delay);
console.log(`Submit scheduled for ${submitTime.toLocaleTimeString()}`);
}
// Call the scheduleSubmit function to start the process
scheduleSubmit();
trying to learn the script in multiple request scenario.
2
Answers
The default behaviour of form in the browser is to trigger a page refresh after the form is submitted. If you want to disable the browser default for form submission, you should call
event.preventDefault()
in yourscheduleSubmit()
function and this will prevent your page from refreshing after the button is clicked.It would have been very helpful if you posted your HTML document too but I hope this solves the refresh issue you’re experiencing.
As the HTML to the question wasn’t provided I took the liberty of inventing my own. In the snippet below the data of the page’s first
<form>
element will be evaluated (shown in aconsole.log()
and sent to a remote server: https://dummyjson.com) at a given time (here: 19:45h local time):I modified OP’s script to not operate on a particular
<button>
but instead to work with the<form>
and its input elements. This way the form will not be submitted as such and a page reload is avoided. If you want to transfer the data to a server at the targeted time, you could use afetch()
call to establish this AJAX communication in the background. I used the publicly available API https://dummyjson.com to send the data to.