Javascript如何重启计时器

时间:2014-01-28 02:42:03

标签: javascript timer

我借用以下代码让5秒计时器工作。现在我需要能够点击播放按钮重新启动计时器。

HTML:

<div class="rightCol">
        <span id="countdown" class="timer"> </span>
        <div>
            <textarea name="passage" id="passage_text"></textarea>
            <input type="button" id="passage_button" value="Publish">
            <input type="button" value="Play" id="play">
        </div>
    </div>

Javascript功能:

var seconds = 5;
function secondPassed() {
    var minutes = Math.round((seconds - 30)/60);
    var remainingSeconds = seconds % 60;
    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds;  
    }
    document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
    if (seconds == 0) {
        clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "Time's Up!";
                document.getElementById("passage_text").disabled = true;

    } else {
        seconds--;
    }
}
 
var countdownTimer = setInterval('secondPassed()', 1000);

现在我已经让play按钮刷新了页面,但必须有更好的方法来执行此操作。我不是很精通JS,所以一些帮助表示赞赏。 jQuery是可以接受的。

1 个答案:

答案 0 :(得分:0)

单击按钮时重置值;

<input type="button" value="Play" id="play" onclick="javascript:seconds=5;">