有没有办法在给定时间段后使用JQuery重定向到特定的URL?
答案 0 :(得分:106)
您可以使用setTimeout()
功能:
// Your delay in milliseconds
var delay = 1000;
setTimeout(function(){ window.location = URL; }, delay);
答案 1 :(得分:51)
你真的不需要jQuery。您可以使用setTimeout方法使用纯JavaScript进行操作:
// redirect to google after 5 seconds
window.setTimeout(function() {
window.location.href = 'http://www.google.com';
}, 5000);
答案 2 :(得分:13)
$(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);
});
答案 3 :(得分:8)
您可以使用
$(document).ready(function(){
setTimeout(function() {
window.location.href = "http://test.example.com/;"
}, 5000);
});
答案 4 :(得分:5)
只需使用:
setTimeout("window.location.href='yoururl';",4000);
..'4000'是m.second
答案 5 :(得分:3)
是的,解决方案是使用setTimeout,如下所示:
var delay = 10000;
var url = "https://stackoverflow.com";
var timeoutID = setTimeout(function() {
window.location.href = url;
}, delay);
请注意,结果存储在timeoutID
中。如果由于某种原因您需要取消订单,您只需要致电
clearTimeout(timeoutID);
答案 6 :(得分:2)
我做了一个简单的演示,提示X秒,并重定向到设置url。如果您不想等到计数结束,只需点击计数器即可立即重定向。 简单的计数器,在页面中间脉冲时间时倒计时。 您可以在单击或加载某些页面时运行它。
<强> LIVE DEMO ONLOAD 强>
<强> LIVE DEMO ONCLICK 强>
我还为此制作了github回购: 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;
}
});
});
答案 7 :(得分:1)
通过以下任一方法使用setTimeout函数
// simulates similar behavior as an HTTP redirect
window.location.replace("http://www.google.com");
// simulates similar behavior as clicking on a link
window.location.href = "http://www.google.com";
setTimeout(function(){
window.location.replace("http://www.google.com");
}, 1000)
答案 8 :(得分:0)
这是@Vicky解决方案的改进-它添加了clearInterval(),以防止在重定向花费太长时间时重新加载循环:
public abstract class MyBase
{
public virtual void MyVirtualMethod() { }
public virtual void MyOtherVirtualMethod() { }
public abstract void MyAbtractMethod();
}
public class MyDerived : MyBase
{
// When overriding a virtual method in MyBase, we use the override keyword.
public override void MyVirtualMethod() { }
// If we want to hide the virtual method in MyBase, we use the new keyword.
public new void MyOtherVirtualMethod() { }
// Because MyAbtractMethod is abstract in MyBase, we have to override it:
// we can't hide it with new.
// For consistency with overriding a virtual method, we also use the override keyword.
public override void MyAbtractMethod() { }
}