JQuery Redirect to URL after specified time

前端 未结 9 957
感动是毒
感动是毒 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

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

提交回复
热议问题