0

所以我试图创建一个简单的游戏,如果用户得到正确答案,FlipClock.js 将停止并记录用户获得正确答案所花费的时间。

作为使用 javascript 的新手,我在这方面遇到了很多麻烦,而且我不知道如何调用函数来停止时钟。任何帮助将不胜感激。

当我运行它时,我收到一条错误消息,说 Uncaught TypeError: Cannot read property 'stop' of undefined in the console window。

这是我的代码:

    <script type="text/javascript">
        var clock;

        $(document).ready(function() {
            var clock;

            clock = $('.clock').FlipClock(180, {
                clockFace: 'MinuteCounter',
                countdown: true,
                callbacks: {
                    stop: function() {
                        $('.message').html('Game Over!')
                    }
                }
            });

        });
    </script>



    <h1>LATEHENP</h1> <!-- ELEPHANT -->



<form id="inputForm">
<p>
<label for="userAnswer">Answer:</label>
<input id="userAsnwer" type="text" name="userAnswer" autocomplete="off" maxlength="8"/>
</p>
<input type="button" value="Submit" id="loginBtn"/>
</form>

<script type="text/javascript">



function process(){
    console.log("working");
        if(inputForm.userAnswer.value.toLowerCase()==="elephant")
    {   //Correct
     document.getElementById("message2").innerHTML = "Well done!";
         clock.stop();
         var time  = clock.getTime();
         console.log("time");

    }
    else
    {
        //Incorrect
    document.getElementById("message2").innerHTML = "Incorrect! <a href='javascript:location.reload(true)'>Retry?</a> ";
    }
};


var btn = document.getElementById("loginBtn");
btn.addEventListener("click", process, false);

</script>
4

1 回答 1

0

看起来您正在尝试在选项区域中定义停止方法。

我将在设置 var clock 的片段之外定义 clock.stop 作为时钟对象的实例化。然后在提交页面后触发您的 process() 函数。

于 2015-07-21T15:30:22.333 回答