What is the best way to add a delay to the form submition with javascript/jquery

I am adapting a codebase and learning javascript/jquery as i go, I need the .submit() to delay for about 5 to 10 seconds so that the answer can be read by the user before the form actually submits the user information.

I tried

$(".profile-form").delay(5000).submit();

thinking this would work, but it isnt.

here is my code snippet, any ideas on how to get the submition delay to work?

var sec = ["25% bonus on your next order! use KEYCODE: TWENTY-FIVE", "15% bonus on your next order! use KEYCODE: FIFTEEN", 
"10% bonus on your next order! use KEYCODE: TEN", "10% bonus on your next order! use KEYCODE: TEN", 
"Free Shipping bonus on your next order! use KEYCODE: FREE SHIPPING", "Free Shipping bonus on your next order! use KEYCODE: FREE SHIPPING"];

var offset = 30;

//set default degree (360*5)
var degree = 1800;
//number of clicks = 0
var clicks = 0;

$(document).ready(function(){
	
	/*WHEEL SPIN FUNCTION*/
	$('#spin').on('click', function handleSpin () {
      if ($('#login').val() === '') {
        return
      }

      $(this).off('click', handleSpin)
      	$(".profile-form").delay(5000).submit();
      
		//add 1 every click
		clicks ++;

It seems that you are misunderstanding the purpose of jQuery’s delay. It’s to be used with jQuery animation effects which come from an internal queue. It doesn’t actually delay anything else.

Just use the normal setTimeout function to delay things.

setTimeout(function () {
    $(".profile-form").submit();
}, 5000);

Or if you want to use the fancier ES6 syntax:

setTimeout(() => $(".profile-form").submit(), 5000);
1 Like

thanks a lot paul, not only did your solution solve the issue, you are helping me to learn javascript/jquery!

thanks so much

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.