2011-01-27 7 views
4

Hy、10、.. 9、...スクリプトでリダイレクト

私は10秒です。遅延ページ。

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

と機能リダイレクト(urlがその)ちょうど私が例えば、小さなメッセージは私のページの下部に表示されている場合いいだろう何

はdocument.location

ん:10秒でリダイレクトを...

9秒で運命へのリダイレクト...

を "1秒後にそののsetTimeoutは解雇" ..

など

ps。最後のドットは左から右に行くと分かります。 ..

私はmyselftがおそらくタイムアウトの毎秒を取得する方法を知り、jqueryだけで番号を変更しました...もしそれがまったく可能ならば。

+0

これはNASAの誓約ですか? –

+0

ha:Dいいえ、..自分自身に問題があります。最初に答えを確認する – PathOfNeo

答えて

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。あなたは、秒(S)のものを持っていると上記の適切な行を交換したい場合は

...

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

、これは英語でうまく動作しますが、おそらく他のと同じように簡単ではありません言語。

+0

詳細なソリューションをありがとう。暴れ! – PathOfNeo

2

あなたはbottomleftの座標で絶対的に位置するdivを作成し、divのテキストを1秒ごとにルーピングして更新することができます。タイムアウトは必ずしも正確に1秒で起動しませんので、それは、不正確なること

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); 
} 
+2

'setTimeout()'の最初の引数として文字列を使うのは、基本的に 'eval()'です。 – alex

+0

ありがとう@alex私はそれを変更しました。 – TheVillageIdiot

1

は秒数で変数を定義し、setTimeoutメソッド(...、1000)コール機能に秒数、いくつかの-DIV /スパンの変更内容を減少しますwhitch。 ..、secs = 0の場合はリダイレクトします。

関連する問題