developer tip

지정된 시간 후에 URL로 JQuery 리디렉션

optionbox 2021. 1. 9. 09:45
반응형

지정된 시간 후에 URL로 JQuery 리디렉션


주어진 기간 후에 JQuery를 사용하여 특정 URL로 리디렉션하는 방법이 있습니까?


다음 setTimeout()기능을 사용할 수 있습니다 .

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

이를 위해 jQuery가 실제로 필요하지 않습니다. setTimeout 메소드를 사용하여 일반 자바 스크립트로 할 수 있습니다 .

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

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

당신이 사용할 수있는

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

다음을 사용하십시오.

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

.. 여기서 '4000'은 m.second입니다.


예, 해결책은 다음과 같이 setTimeout 을 사용하는 것입니다 .

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

결과는에 저장되었습니다 timeoutID. 어떤 이유로 든 주문을 취소해야하는 경우

clearTimeout(timeoutID);

X 초를 입력하라는 메시지를 표시하고 URL을 설정하도록 리디렉션하는 간단한 데모를 만들었습니다. 카운트가 끝날 때까지 기다리지 않으려면 카운터를 클릭하여 시간없이 리디렉션하십시오. 페이지 중앙에서 시간을 펄싱하면서 카운트 다운하는 간단한 카운터입니다. 클릭시 또는 일부 페이지가로드되는 동안 실행할 수 있습니다.

라이브 데모 온로드

라이브 데모 ONCLICK

나는 또한 이것을 위해 github repo를 만들었습니다 : https://github.com/GlupiJas/redirect-counter-plugin

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

});

참조 URL : https://stackoverflow.com/questions/7276677/jquery-redirect-to-url-after-specified-time

반응형