2011-01-27 85 views
4

Hy,重定向10,.. 9,...脚本

我有10秒的时间。延迟页面。

var t = window.setTimeout('redirect(strUrl)', 11000); 

和功能重定向(strUrl)少了点document.location

将是很好,如果我有一个小消息,在我的页面底部显示,例如:在10秒重定向...

“一秒钟后说的setTimeout炒鱿鱼”

...重定向到命运的9秒..

ps。并且最后的点从左到右你知道它。 .. ...

我自己可能会找出如何让每秒超时,只是用jquery改变了数字...如果这是可能的。

+0

这是一个美国国家航空航天局的分配​​;)? – 2011-01-27 11:27:22

+0

ha:D不,只是有自己的问题。 ..首先检查答案 – PathOfNeo 2011-01-27 11:32:09

回答

6
var timeout = 11; // in seconds 

var msgContainer = $('<div />').appendTo('body'), 
    msg = $('<span />').appendTo(msgContainer), 
    dots = $('<span />').appendTo(msgContainer); 

var timeoutInterval = setInterval(function() { 

    timeout--; 

    msg.html('Redirecting in ' + timeout + ' seconds'); 

    if (timeout == 0) { 
     clearInterval(timeoutInterval); 
     redirect(strUrl); 
    } 

}, 1000); 

setInterval(function() { 

    if (dots.html().length == 3) { 
     dots.html(''); 
    } 

    dots.html(function(i, oldHtml) { return oldHtml += '.' }); 
}, 500); 

See it on jsFiddle

如果你想有秒的东西,代替上面相应的行...

msg.html('Redirecting in ' + timeout + ' second' + ((timeout != 1) ? 's' : '')); 

当然,这个效果很好的英语,但可能是不与其他容易语言。

+0

感谢您的详细解决方案。横冲直撞! – PathOfNeo 2011-01-27 13:43:59

2

你可以通过创建一个div来完成,你可以用bottomleft坐标绝对定位,并且通过在每秒循环来更新div中的文本。事情是这样的:

function redirect(strurl) { 
    var seconds = 10; 
    var div = $("<div/>").css({ 
    position: "absolute", 
    left: "0px", 
    bottom: "0px" 
    }).appendTo(document.body); 

    continueCountdown(); 

    function continueCountdown() { 
    --seconds; 
    if (seconds >= 0) { 
     div.text("...Redirecting in " + seconds + " seconds..."); 
     setTimeout(continueCountdown, 1000); 
    } 
    else { 
     // Redirect here 
     document.location = strurl; 
    } 
    } 
} 

Live example

需要注意的是,这将是不精确,因为超时不一定会在正好一秒。但它会很接近。

2

试试这个:

var count=10; 

window.setTimeout(function(){redirect(url)}, 1000); 

function redirect(url){ 
    if(--count==0){ 
     location.href=url; 
     return; 
    } 
    $("#DIV_TO_DISPLAY_MESSAGE").text("redirecting in " + count+" secs."); 
    window.setTimeout(function(){redirect(url)}, 1000); 
} 
1

定义一个变量秒的数量,并在的setTimeout(...,1000)通话功能whitch会降低秒的数量,一些-DIV/SPAN的变化内容。 ..,如果秒= 0重定向。