How to insert a set time into a function

Asked

Viewed 103 times

4

The function of this code below and next after I click on a form Ubmit it redirects to another page this worked now just need it to redirect to another page after, 5 seconds someone knows how to do this manjo very little javascript?

$("form").submit(function(){
  window.location = 'minha URL';
}
  • Use setTimeout

  • You can show me what the code would look like

  • Utilize setTimeout(function(){ window.location = 'minha URL'; }, 5000);

2 answers

4

You can use setTimeout()

Do so:

$("form").submit(function(){
    setTimeout(function() {
        window.location = 'minha URL';
    }, 5000);
}

After clicking the button will execute the function setTimeout() which receives a function to be executed and the time in milliseconds, after that time it executes the function passed.

You can still assign the function to a variable to cancel later, for that you would use the function clearTimeout() receiving the variable with the function to be canceled:

$("form").submit(function(){
    var timeout = setTimeout(function() {
        window.location = 'minha URL';
    }, 5000);

    //Parar timeout
    clearTimeout(timeout);
}

3


$("form").submit(function(){
  setTimeout(function(){ window.location = 'minha URL'; }, 5000);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.