programing

자바스크립트를 사용하여 X초 대기 후 페이지 리디렉션

skycolor 2023. 10. 24. 21:09
반응형

자바스크립트를 사용하여 X초 대기 후 페이지 리디렉션

오류 메시지를 넣은 후 5초 후에 특정 url로 리디렉션해야 합니다.우선 아래와 같이 자바스크립트를 사용하였습니다.

document.ready(window.setTimeout(location.href = "https://www.google.co.in",5000));

하지만 그것은 5초 동안 기다리는 것이 아닙니다.웹 브라우저가 아닌 DOM에서 문서가 로드될 때 "document.ready()"가 호출되는 것을 구글에서 검색해보니 문제가 발생했습니다.

jQuery의 window.load() 기능을 사용했지만 여전히 원하는 것을 얻지 못하고 있습니다.

$(window).load(function() {
               window.setTimeout(window.location.href = "https://www.google.co.in",5000);
            });

5초 동안 기다리려면 정확히 어떻게 해야 하는지 알려줄 수 있는 사람?

거의 다 온 것 같네요.시도:

if(error == true){

    // Your application has indicated there's an error
    window.setTimeout(function(){

        // Move to a new location or you can do something else
        window.location.href = "https://www.google.co.in";

    }, 5000);

}

당신은 실제로 그 안에 있는 기능을 전달해야 합니다.window.setTimeout()5000밀리초 후에 실행할 방법은 다음과 같습니다.

$(document).ready(function () {
    // Handler for .ready() called.
    window.setTimeout(function () {
        location.href = "https://www.google.co.in";
    }, 5000);
});

자세한 내용:

이 자바스크립트 기능을 사용할 수 있습니다.여기서 리디렉션 메시지를 사용자에게 표시하고 지정된 URL로 리디렉션할 수 있습니다.

<script type="text/javascript">   
    function Redirect() 
    {  
        window.location="http://www.newpage.com"; 
    } 
    document.write("You will be redirected to a new page in 5 seconds"); 
    setTimeout('Redirect()', 5000);   
</script>

다음으로 함수를 전달해야 합니다.setTimeout

$(window).load(function () {
    window.setTimeout(function () {
        window.location.href = "https://www.google.co.in";
    }, 5000)
});

자바스크립트 사용setInterval()지정된 시간이 지난 후 페이지를 리디렉션하는 메서드입니다.다음 스크립트는 5초 후 페이지를 리디렉션합니다.

var count = 5;
setInterval(function(){
    count--;
    document.getElementById('countDown').innerHTML = count;
    if (count == 0) {
        window.location = 'https://www.google.com'; 
    }
},1000);

예제 스크립트 및 라이브 데모는 여기에서 확인 가능 - 자바스크립트를 사용하여 지연페이지 재연결

$(document).ready(function() {
    window.setTimeout(function(){window.location.href = "https://www.google.co.in"},5000);
});
setTimeout('Redirect()', 1000);
function Redirect() 
{  
    window.location="https://stackoverflow.com"; 
} 

//document.write("1000 -> 1초, 2000 -> 2초 후에 새 페이지로 리디렉션됩니다");

<script type="text/javascript">
      function idleTimer() {
    var t;
    //window.onload = resetTimer;
    window.onmousemove = resetTimer; // catches mouse movements
    window.onmousedown = resetTimer; // catches mouse movements
    window.onclick = resetTimer;     // catches mouse clicks
    window.onscroll = resetTimer;    // catches scrolling
    window.onkeypress = resetTimer;  //catches keyboard actions

    function logout() {
        window.location.href = 'logout.php';  //Adapt to actual logout script
    }

   function reload() {
          window.location = self.location.href;  //Reloads the current page
   }

   function resetTimer() {
        clearTimeout(t);
        t = setTimeout(logout, 1800000);  // time is in milliseconds (1000 is 1 second)
        t= setTimeout(reload, 300000);  // time is in milliseconds (1000 is 1 second)
    }
}
idleTimer();
        </script>

언급URL : https://stackoverflow.com/questions/17150171/page-redirect-after-x-seconds-wait-using-javascript

반응형