2015-05-09 32 views
0

在這裏我有一個5秒後重定向用戶一個簡單的倒計時功能。的JavaScript:變量未定義內的if else

<script type="text/javascript"> 

var timer = 5; 
var counter = document.getElementById("countdown"); 

function countdown(target) 
{ 
    counter.innerHTML = timer--; 
    if (timer >= 0) 
    { 
     setTimeout(function() { 
      countdown(); 
     }, 1000); 
    } 
    else 
    { 
     // Redirect the user 

     console.log(target); // Outputs "undefined" after 5 seconds 

     if (!typeof target === "undefined") 
     { 
      window.location.href = target; 
     } 
    } 
}; 

var target = "/account/"; 
countdown(target); 

</script> 

的問題是,如果將5秒後,它的時間window.location.href做重定向,「目標」變量是不確定的,我不知道爲什麼會這樣。

我已經研究和嘗試了各種解決方案,即使設置了「目標」的設置與「定時器」和「計數器」,但沒有奏效。我的目標是在每個功能實例上設置不同的「目標」。

UPDATE:設置countdown();countdown(target);固定的「不確定」的問題,但window.location.href對象仍然不重定向到target變量值,可以做些什麼來解決這個問題?

+0

程序中的「str」是什麼? – thefourtheye

+0

對不起,它是「目標」,而不是更新後。 –

+4

您沒有將任何參數傳遞給'setTimeout'回調中的'countdown'。 – user2357112

回答

1

您需要將目標倒計時的setTimeout

function countdown(target) 
{ 
    counter.innerHTML = timer--; 
    if (timer >= 0) 
    { 
     setTimeout(function() { 
      countdown(target); 
     }, 1000); 
    } 
    else 
    { 
     // Redirect the inactive user to the homepage 

     console.log(target); // Outputs "undefined" after 5 seconds 

     if (!typeof target === "undefined") 
     { 
      window.location.href = target; 
     } 
    } 
}; 
+0

我已經接受了這個作爲回答,因爲它解決了「目標」是「不確定」的初始問題,但我鼓勵讀者還檢查了我的回答如下(解決了全部發行),並給予好評,如果它幫助。 –

0

這是你的問題,當您再次調用倒計時功能,您應該發送「目標」對象。

setTimeout(function() { 
     countdown(); // should be: countdown(target); 
    }, 1000); 
+0

嘗試過,它將「目標」設置爲正確的值,但「其他」中的重定向仍然不起作用。 –

0

試試這個,它的工作,你忘了在你的函數中傳遞參數。

var timer = 5; 
var counter = document.getElementById("countdown"); 

function countdown(timer,target){ 

counter.innerHTML = timer--; 
if (timer > 0) 
{ 
    setTimeout(function() { 
     countdown(timer,target); 
    }, 1000); 
} 
else 
{ 
    // Redirect the inactive user to the homepage 

    console.log(target); // Outputs "undefined" after 5 seconds 

    if (!typeof str === "undefined") 
    { 
     window.location.href = target; 
    } 
} 
}; 

var target = "/account/"; 

countdown(timer,target); 
+0

爲什麼你在函數參數中加入了「timer」?計時器以這種方式工作,並按預期遞減。問題是「window.location.href」沒有重定向到「目標」值。 –

0

嘗試使用完整的URL,而不是使用絕對URL重定向等代替/account/使用localhost/account/

UPDATE這必須工作,我已經測試過它

<span id="countdown"></span> 
<script> 

var timer = 5; 
var counter = document.getElementById("countdown"); 

function countdown(target) 
{ 
counter.innerHTML = timer--; 
if (timer >= 0) 
{ 
    setTimeout(function() { 
     countdown(target); 
    }, 1000); 
} 
else 
{ 
    // Redirect the inactive user to the homepage 

    console.log(target); // Outputs "undefined" after 5 seconds 


     window.location.href = target; 

} 
}; 

var target = "/account/"; 
countdown(target); 
</script> 
+0

絕對URL是你建議我使用的(完整網址),目前我使用的是所謂的相對URL,這不是解決方案,請檢查我發佈的答案。 –

1

明白了,對於一些因此,if (!typeof target === "undefined")還是回到真實的,即使target確實規定,造成window.location.href不火起來,並重定向到t他值target

UPDATE:我這樣做是錯誤的,它是一個分組問題,但使用if (typeof target !== "undefined")使它按預期工作。

我分享固定的代碼的情況下,它可以幫助別人:

<script type="text/javascript"> 

var timer = 5; 
var counter = document.getElementById("countdown"); 

function countdown(target) 
{ 
    counter.innerHTML = timer--; 
    if (timer >= 0) 
    { 
     setTimeout(
      function() 
      { countdown(target); }, 1000 
     ); 
    } 
    else 
    { 
     // Redirect the user 
     if (typeof target !== "undefined") 
     { 
      window.location.href = target; 
     } 
    } 
}; 

var target = "/MixaPay/"; 
countdown(target); 

</script> 

感謝大家誰作出了貢獻。

+0

[該表達式因爲被分組爲'(!(typeof target))===「undefined」而不起作用。](https://developer.mozilla.org/en-US/docs/Web/JavaScript/ Reference/Operators/Operator_Precedence)使用'!=='。 – user2357112

+0

謝謝,我不知道爲什麼我沒有想到這樣做,完美的作品。 –