2013-06-17 2 views
50

오류 메시지를 보낸 후 5 초 후에 특정 URL로 리디렉션해야합니다. 먼저 자바 스크립트를 아래와 같이 사용했습니다.페이지 X 초 후에 리디렉션 JavaScript를 사용하여 대기

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

그러나 5 초 동안 기다리지 않습니다. DOM에서 문서가로드 될 때 "document.ready()"가 호출된다는 것을 알기보다는 Google에서 문제를 검색했습니다. 웹 브라우저가 아니라 DOM에로드됩니다.

jQuery의 window.load() 함수를 사용했지만 여전히 내가 원하는 것을 얻지 못했습니다.

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

아무에게 나 5 초 동안 기다려야 할 필요가있는 경우 알려 주시기 바랍니다.

+0

: [JS 리디렉션 (http://insider.zone/tools/client-side-url-redirect- 발전기 /) –

답변

76

거의 다 왔어. 시도해보십시오 :

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

} 
27

당신은 실제로 이런 식으로, 당신은 5000 밀리 초 후에 실행하려는 window.setTimeout() 내부 함수를 전달해야

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

대한 추가 정보를 원하시면 : .setTimeout()

+1

나를 이길. 위치가 즉시 할당되기 때문에 리디렉션이 대기하지 않습니다. 함수 호출이어야합니다. – Cfreak

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

당신이를 통과해야 기능을 setTimeout

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

이 자바 스크립트 기능을 사용할 수 있습니다. 여기서 사용자에게 리디렉션 메시지을 표시하고 지정된 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> 
3

JavaScript를 사용하면 지정된 시간 후에 페이지를 리디렉션 할 수 있습니다. setInterval() 다음 스크립트는 5 초 후에 페이지를 리디렉션합니다.

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

예 스크립트와 라이브 데모를 여기에서 찾을 수 있습니다 - 완벽한 솔루션에 대한 Redirect page after delay using JavaScript

관련 문제