1)我的计时器有问题。它在屏幕加载时启动,它应该从按钮START开始。 2)如果没有。当计时器到达00:00时,如果我赢了或输了,它应该显示消息(警报)。 http://prntscr.com/ffi4bu墙值不断变化。我的代码没有用,但你明白了。
1)JS代码计时器
function startTimer(duration, display) {
var timer = duration,minutes, seconds;
var timers = setInterval(runner, 1000);
function runner() {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (--timer < 0) {
clearInterval(timers)
}
}
}
window.onload = function() {
var oneMinutes = 60 * 1,
display = document.querySelector('#time');
startTimer(oneMinutes, display);
};
HTML
<div class="timer" ><b>Battle time: <span id="time"></span></b></div>
<BUTTON class="begin" onclick="startTimer(60,document.querySelector('#time'))"><b>START</b></BUTTON>
2)JS
function finish(){
var g = function startTimer;
var h = document.getElementById('wallvalue');
if (function startTimer = 0 )&& ( h<500 ) {
alert("You won!");
}
else {
alert("You lost!");
}
}
答案 0 :(得分:0)
如果要在按钮单击时调用,请从window.onload中删除startTimer函数调用。否则它将在窗口加载后立即加载。
window.onload = function() {
var oneMinutes = 60 * 1,
display = document.querySelector('#time');
//startTimer(oneMinutes, display);
};
在startTimer内部对“零”值调用完成并从完成中删除不明确的函数调用行。还要检查“h”,因为当定时器值已经为零时将调用“finsh”。
function startTimer(duration, display) {
var timer = duration,minutes, seconds;
var timers = setInterval(runner, 1000);
function runner() {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (--timer <= 0) {
clearInterval(timers)
finish()
}
}
}
function finish(){
var h = document.getElementById('wallvalue');
if ( h<500 ) {
alert("You won!");
}
else {
alert("You lost!");
}
}