Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery在指定时间后重定向到URL_Javascript_Jquery_Redirect - Fatal编程技术网

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

Javascript JQuery在指定时间后重定向到URL,javascript,jquery,redirect,Javascript,Jquery,Redirect,有没有办法使用JQuery在给定时间段后重定向到特定URL?您可以使用以下功能: // Your delay in milliseconds var delay = 1000; setTimeout(function(){ window.location = URL; }, delay); 您并不需要jQuery来完成这个任务。您可以使用以下方法使用纯javascript完成此操作: 只需使用: setTimeout("window.location.href='yoururl';",400

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

您可以使用以下功能:

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

您并不需要jQuery来完成这个任务。您可以使用以下方法使用纯javascript完成此操作:

只需使用:

setTimeout("window.location.href='yoururl';",4000);
…其中“4000”是米秒

您可以使用

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

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

我还为此进行了github回购:

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;
        }
    });

});

是的,解决方案是这样使用:

var delay = 10000;
var url = "https://stackoverflow.com";
var timeoutID = setTimeout(function() {
    window.location.href = url;
}, delay);
请注意,结果存储在
timeoutID
中。如果出于任何原因您需要取消订单,您只需要打电话

clearTimeout(timeoutID);

这是经过改进的@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);
                            });

将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) 
资料来源:

我不鼓励使用
eval()
,因为它有许多危险的后果。相反,我建议使用
parseInt()
。唯一的问题是,如果重定向不快速发生,它会继续重定向,并且永远不会到达目标页面,您应该接受正确的答案……如果您想要跨浏览器和seo支持:这如何解决问题的第二部分(…在给定时间段后)?使用setTimeout功能更新答案
                            $(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);
                            });
// 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)