Javascript JQuery 在指定时间后重定向到 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7276677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:36:04  来源:igfitidea点击:

JQuery Redirect to URL after specified time

javascriptjqueryredirect

提问by Brian

Is there a way to use JQuery to redirect to a specific URL after a give time period?

有没有办法在给定的时间段后使用 JQuery 重定向到特定的 URL?

回答by Rion Williams

You could use the setTimeout()function:

您可以使用该setTimeout()功能:

// Your delay in milliseconds
var delay = 1000; 
setTimeout(function(){ window.location = URL; }, delay);

回答by Darin Dimitrov

You don't really need jQuery for this. You could do it with plain javascript using the setTimeoutmethod:

为此,您实际上并不需要 jQuery。您可以使用setTimeout方法使用纯 javascript 来完成:

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

回答by Vicky

$(document).ready(function() {
    window.setInterval(function() {
    var timeLeft    = $("#timeLeft").html();
        if(eval(timeLeft) == 0) {
                window.location= ("http://www.technicalkeeda.com");
        } else {
            $("#timeLeft").html(eval(timeLeft)- eval(1));
        }
    }, 1000);
});

回答by Mandeep Singh

You can use

您可以使用

    $(document).ready(function(){
      setTimeout(function() {
       window.location.href = "http://test.example.com/;"
      }, 5000);
    });

回答by Attila Nagy

just use:

只需使用:

setTimeout("window.location.href='yoururl';",4000);

..where the '4000' is m.second

..其中'4000'是m.second

回答by Lajos Arpad

Yes, the solution is to use setTimeout, like this:

是的,解决方案是使用setTimeout,如下所示:

var delay = 10000;
var url = "https://stackoverflow.com";
var timeoutID = setTimeout(function() {
    window.location.href = url;
}, delay);

note that the result was stored into timeoutID. If, for whatever reason you need to cancel the order, you just need to call

请注意,结果存储在timeoutID. 如果,无论出于何种原因您需要取消订单,您只需致电

clearTimeout(timeoutID);

回答by DevWL

I have made a simple demo that prompts for X number of seconds and redirects to set url. If you don't want to wait until the end of the count, just click on the counter to redirect with no time. Simple counter that will count down while pulsing time in the midle of the page. You can run it onclick or while some page is loaded.

我做了一个简单的演示,提示 X 秒数并重定向以设置 url。如果您不想等到计数结束,只需单击计数器即可立即重定向。简单的计数器,将在页面中间脉冲时间时倒计时。您可以在单击时或在加载某些页面时运行它。

LIVE DEMO ONLOAD

现场演示加载

LIVE DEMO ONCLICK

现场演示点击

I also made github repo for this: https://github.com/GlupiJas/redirect-counter-plugin

我还为此制作了 github repo:https: //github.com/GlupiJas/redirect-counter-plugin

JS CODE EXAMPLE:

JS代码示例:

// FUNCTION CODE
function gjCountAndRedirect(secounds, url)
{

        $('#gj-counter-num').text(secounds);

        $('#gj-counter-box').show();

    var interval = setInterval(function()
    {

        secounds = secounds - 1;

        $('#gj-counter-num').text(secounds);

        if(secounds == 0)
        {

            clearInterval(interval);
            window.location = url;
            $('#gj-counter-box').hide();

        }

    }, 1000);

    $('#gj-counter-box').click(function() //comment it out -if you dont want to allo count skipping
    {
        clearInterval(interval);
        window.location = url;

    });
}

// USE EXAMPLE
$(document).ready(function() {
    //var
    var gjCountAndRedirectStatus = false; //prevent from seting multiple Interval

    //call
    $('h1').click(function(){
        if(gjCountAndRedirectStatus == false)
        {
            gjCountAndRedirect(10, document.URL);
            gjCountAndRedirectStatus = true;
        }
    });

});

回答by hydra

Use setTimeout function with either of the following

使用 setTimeout 函数与以下任一

// simulates similar behavior as an HTTP redirect
window.location.replace("http://www.google.com");

// simulates similar behavior as clicking on a link
window.location.href = "http://www.google.com";

setTimeout(function(){
   window.location.replace("http://www.google.com");
}, 1000) 

source: https://www.sitepoint.com/jquery-redirect-web-page/

来源:https: //www.sitepoint.com/jquery-redirect-web-page/

回答by nickornotto

This is improved @Vicky solution - it has clearInterval() added so it prevents a loop of reloading if the redirect takes too long:

这是改进的@Vicky 解决方案 - 它添加了 clearInterval(),因此如果重定向时间过长,它可以防止重新加载循环:

                            $(document).ready(function () {
                                var myTimer = window.setInterval(function () {
                                    var timeLeft = $("#countdown").html();
                                    if (eval(timeLeft) == 0) {
                                        console.log('Now redirecting');
                                        clearInterval(myTimer);
                                        window.location = ("@Html.Raw(HttpUtility.HtmlDecode(redirectUrl))");
                                    } else {
                                        $("#countdown").html(eval(timeLeft) - eval(1));
                                    }
                                }, 1000);
                            });