我刚刚想出了如何使用javascript来回显我的重定向页面的动态倒计时。我很好奇当倒计时只剩 1 秒时如何回响"秒",但我该怎么做?
这是我的代码:
<script>
var counter = 5;
setInterval (function()
{
counter--;
if(counter < 1)
{
window.location = 'login.php';
}
else
{
document.getElementById("count").innerHTML = counter;
}
}, 1000);
</script>
--
echo "<table><tr><td> You will be redirected in <div id='"count'">5</div> seconds.</td></tr></table>";
它回显"在 5/4/3/2/1 秒内重定向"。OC 原来如此,当倒计时达到 1 时,我对那个 S 感到困扰。任何帮助将不胜感激。
将
"s"设置为变量。 如果计数器 == 1,请将该变量设置为空字符串,然后相应地更新您的div:
function() {
var plural = "s";
if (counter == 1) {
plural = "";
}
// update div
}
由于您尚未发布"秒"的位置,因此我假设它在 DIV 之后。我建议在你的javascript中使用一些if语句来倒计时并相应地设置innerHTML:
<script>
var counter = 5;
var seconds = "";
setInterval (function()
{
counter--;
if(counter < 1)
{
window.location = 'login.php';
}
else
{
if(count <= 1) seconds = " second";
else seconds = " seconds";
document.getElementById("count").innerHTML = counter + seconds;
}
}, 1000);
</script>
<div id='"count'">5</div>