我试图通过从 1 分钟倒计时到 0 秒来在我的网页上创建超时,然后显示一条消息。如果用户移动鼠标(即在页面上仍处于活动状态),则计时器将重置。我无法获得重置功能来重置我的值。它正在做的是比以前更快地倒计时并陷入牢不可破的循环中。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
var mins = 1;
var secs = 0;
var timer;
function start()
{
timer = setInterval("update()", 1000);
}
function reset() {
var mins = 1
var secs = 0;
var timer;
start();
}
function update()
{
var timeField = document.getElementById("time");
if (secs == 0)
{
if (mins == 0)
{
timeField.innerHTML = "Time's up!";
clearInterval(timer);
alert("Time's up");
return;
}
mins--;
secs = 59;
}
else
{
secs--;
}
if (secs < 10)
{
timeField.innerHTML = 'Time left: ' + mins + ':0' + secs;
}
else
{
timeField.innerHTML = 'Time left: ' + mins + ':' + secs;
}
}
</script>
</head>
<body onload="start();" onmousemove="reset();">
<div id="time" >
</div>
</body>
</html>
我怎样才能让它重置倒计时并重新开始?我是javascript新手,所以请耐心等待。