所以我试图创建一个简单的游戏,如果用户得到正确答案,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>