我使用 java 脚本制作了一个反向计时器。我分别添加了启动和重置按钮,但需要在单击时更改按钮功能。如果我点击开始,那么开始功能应该运行,并且在开始的地方应该出现重置按钮,在再次点击重置后,重置功能应该开始并且应该出现开始按钮。我怎样才能做到这一点。请建议。
这是代码
var interval;
var minutes = 1;
var seconds = 00;
function countdown(element) {
interval = setInterval(function() {
var el = document.getElementById(element);
if(seconds == 0) {
if(minutes == 0) {
alert(el.innerHTML = "countdown's over!");
clearInterval(interval);
return;
} else {
minutes--;
seconds = 60;
}
}
if(minutes > 0) {
var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
} else {
var minute_text = '';
}
var second_text = seconds > 1 ? 'seconds' : 'second';
el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining';
seconds--;
}, 1000);
}
var renew = document.getElementById('renew');
renew.onclick = function() {
minutes = 1;
seconds = 00;
clearInterval(interval);
interval = null;
countdown('countdown');
}
var start = document.getElementById('start');
start.onclick = function() {
document.getElementById('renew').onclick = function (event){
<input type="button" value="renew" id="renew" />
};
if (!interval) {
countdown('countdown');
}
}
html代码是:
<div id="topmain">
<div style="margin-left:15px; float:left;">
<input type="button" value="start" id="start" />
<input type="button" value="renew" id="renew" />
<div id='countdown' style="color:#0FC; margin-left:5px; " >
</div> </div>