使用 JavaScript 等待 X 秒后进行页面重定向
Page Redirect after X seconds wait using JavaScript
我需要在放置错误消息后 5 秒后重定向到特定 url。首先,我使用了Javascript,如下所示。
document.ready(window.setTimeout(location.href = "https://www.google.co.in",5000));
但它不会等待 5 秒。比我在谷歌中搜索问题,而不是知道"document.ready(("是在DOM加载文档时调用的,而不是在Web浏览器上。
比我使用了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);
}
您实际上需要在 5000 毫秒后执行的window.setTimeout()
内传递一个函数,如下所示:
$(document).ready(function () {
// Handler for .ready() called.
window.setTimeout(function () {
location.href = "https://www.google.co.in";
}, 5000);
});
更多信息: .setTimeout()
你可以使用这个 JavaScript 函数。在这里,您可以向用户显示重定向消息并重定向到给定的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)
});
使用 JavaScript setInterval()
方法在指定时间后重定向页面。以下脚本将在 5 秒后重定向页面。
var count = 5;
setInterval(function(){
count--;
document.getElementById('countDown').innerHTML = count;
if (count == 0) {
window.location = 'https://www.google.com';
}
},1000);
示例脚本和现场演示可以从这里找到 - 使用 JavaScript 延迟后重定向页面
$(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>
相关文章:
- 通过javascript重定向html传递php变量
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 捕获所有常见类型的重定向、头、元、JavaScript等
- Javascript移动站点重定向问题
- javascript:发送带有音频文件的POST,然后重定向到新页面
- 使用CSS和Javascript重定向-手持设备
- 重定向javascript中的Url
- Servlet仅在单击JavaScript警报消息后重定向
- 登录后重定向,缓存页面问题-Javascript
- 从Ajax脚本重定向Javascript页面
- 重定向Javascript中包含的URL路径
- 重定向javascript,替换url
- 右键单击时重定向Javascript
- 在重定向JavaScript时从URL中删除#
- HTTP重定向-Javascript或Flask
- 使用POST方法重定向javascript页面
- 基于URL的重定向-JavaScript
- 重定向javascript表单
- 这是一个很好的移动重定向javascript吗?或者我会被谷歌封杀
- 如果网页 URL 链接失败,则重定向 Javascript URL