JQuery Redirect to URL after specified time

前端 未结 9 931
感动是毒
感动是毒 2020-12-07 17:20

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

相关标签:
9条回答
  • 2020-12-07 17:53
    $(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);
    });
    
    0 讨论(0)
  • 2020-12-07 17:53

    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.

    LIVE DEMO ONLOAD

    LIVE DEMO ONCLICK

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

    JS CODE EXAMPLE:

    // 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;
            }
        });
    
    });
    
    0 讨论(0)
  • 2020-12-07 17:54

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

                                $(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);
                                });
    
    0 讨论(0)
  • 2020-12-07 18:00

    You could use the setTimeout() function:

    // Your delay in milliseconds
    var delay = 1000; 
    setTimeout(function(){ window.location = URL; }, delay);
    
    0 讨论(0)
  • 2020-12-07 18:02

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

    // redirect to google after 5 seconds
    window.setTimeout(function() {
        window.location.href = 'http://www.google.com';
    }, 5000);
    
    0 讨论(0)
  • 2020-12-07 18:05

    You can use

        $(document).ready(function(){
          setTimeout(function() {
           window.location.href = "http://test.example.com/;"
          }, 5000);
        });
    
    0 讨论(0)
提交回复
热议问题