如何验证倒计时是否达到0

时间:2013-12-06 11:49:24

标签: javascript

我想验证来自Mike Gieson倒计时的计时器如果为0则加载下一页如果还是要发消息,直到现在我有下一个代码

var a = new Countdown({time: 25,
                                    width:100,
                                    height:40,
                                    style       : "boring",
                                    onComplete  : countdownComplete ,
                                    rangeHi:"second"
                                    });
function countdownComplete()
    {
      window.location.href='start.php' ;
    }
window.onbeforeunload = function()
{
  return "Are you shure you whant to leave this page? ";
}

2 个答案:

答案 0 :(得分:0)

您不需要发生事件然后显示消息。只需在页面加载时将消息发送到html元素:

    window.onload = function () {
        document.getElementById("messageID").innerHTML = "Your message goes here.";
    }

并在计时器收到0时删除消息。

    function countdownComplete() {
        //Remove message
        document.getElementById("messageID").innerHTML = "";
        window.location.href = 'start.php';
    }

答案 1 :(得分:0)

您可以在倒计时onComplete事件触发时设置一个标志,并查看beforeunload事件上的标志。如果设置了标志,则倒计时已经完成。

这样的事情:

<html>
    <head>
        <script type="text/javascript" src="countdown.js"></script>
        <script>
            (function() {
                var countDownCompleted = false;             

                var countdown = new Countdown({time: 25,
                    width:100,
                    height:40,
                    style:"boring",
                    onComplete: countdownComplete ,
                    rangeHi:"second"
                });
                function countdownComplete(){
                    countDownCompleted = true;
                    window.location.href='start.php' ;
                }
                window.onbeforeunload = function() {
                    if(!countDownCompleted) {
                        return "Are you sure you want to leave this page? ";.
                    }
                }
        })();
        </script>
    </head>
    <body>
        <a href='start.php'>Go to Start</a> and you will get a message asking if you really want to leave the page
    </body>
</html>