我有一个计时器,它工作得很好,但我需要能够在此之后暂停和恢复它。如果有人能帮助我,我将不胜感激。
<html>
<head>
<script>
function startTimer(m,s)
{
document.getElementById('timer').innerHTML= m+":"+s;
if (s==0)
{
if (m == 0)
{
return;
}
else if (m != 0)
{
m = m-1;
s = 60;
}
}
s = s-1;
t=setTimeout(function(){startTimer(m,s)},1000);
}
</script>
</head>
<body>
<button onClick = "startTimer(5,0)">Start</button>
<p id = "timer">00:00</p>
</body>
</html>
clearTimeout
时,您可以更准确地反映您暂停时的位置,以便在恢复时使用。如果您正在使用日期对象,请记录暂停时间和恢复时间,您将能够计算出差异并进行适当的调整。 - Paul S.