简体   繁体   English

单击按钮后,计算x秒,然后使用jquery重定向到特定链接

[英]After click on a button, count x seconds and then redirect to a specific link using jquery

What i want to do is exactly what i mentioned in the question title. 我想做的正是我在问题标题中提到的内容。 I have a form with a submit button. 我有一个带有提交按钮的表格。 Is it possible using jQuery to redirect the user to a specific link page after 5 seconds starting count from the moment this user clicked the submit button? 从该用户单击“提交”按钮开始算起的5秒钟后,是否可以使用jQuery将用户重定向到特定链接页面?

You can use setTimeout and window.location 您可以使用setTimeout和window.location

setTimeout(function(event) {
  window.location.href = url
    }, 5000);

The below code do exactly what you want: 下面的代码正是您想要的:

$('#button').click(function() {
    setTimeout(function() {
        window.location.href = 'http://new-url';
    }, 5000);
})

This sounds like a job for JavaScript's setTimeout functionality. 这听起来像JavaScript的setTimeout功能的工作。 The first thing you'll need to do is prevent the form submitting immediately (default behaviour) with event.preventDefault() otherwise nothing will be executed. 您需要做的第一件事是防止使用event.preventDefault()立即提交表单(默认行为),否则将不执行任何操作。

Check out this example with comments, Should provide a bit more information for you; 查看带有注释的示例,应为您提供更多信息;

<form method="post">
    <input type="text" name="text-field" placeholder="Enter something" />
    <button type="submit">Submit form</button>
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
    $('form').on('submit', function(event) { // pass an event argument so we can call preventDefault
        event.preventDefault(); // call event.preventDefault to stop form submitting immediately 

        setTimeout(function() { // Set up a setTimeout operation
            console.log('form submitted after timeout');
            window.location.href = '/';
        }, 5000); /// 5000 ms == 5s
    });
});

You need to use setTimeout in order to achieve what you want to achieve. 您需要使用setTimeout才能实现想要实现的目标。 setTimeout is a function which takes two parameters: setTimeout是一个带有两个参数的函数:

  • a callback function 回调函数
  • the timeout in milliseconds 超时(以毫秒为单位)

Callback 打回来

A callback function is a function to be executed when a given event happens. 回调function是在给定事件发生时要执行的function In our case the event is that a given amount of time elapsed. 在我们的案例中,事件是经过了给定的时间。 This is how your callback could look like: 这是您的回调的样子:

function redirect() {
    window.location.href = 'http://www.google.com';
}

Timeout 暂停

The timeout is the second parameter of the function. 超时是函数的第二个参数。 It defines the amount of time to elapse before the callback is being called. 它定义了在调用回调之前经过的时间。 In our case you want 5 seconds to wait before the callback is executed, so the timeout will be 5000 milliseconds. 在我们的示例中,您希望在执行回调之前等待5秒钟,因此超时将为5000毫秒。

Usage 用法

setTimeout(function() {
    window.location.href = 'http://www.google.com';
}, 5000);

or 要么

function redirect() {
    window.location.href = 'http://www.google.com';
}
setTimeout(redirect, 5000);

Either way it is elegant to wrap a function around the solution and call the function on the onclick . 无论哪种方式,都可以将function包装在解决方案周围,然后在onclick上调用该function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM