Javascript Onclick重置设置间隔 功能计时器(x){ 如果(x==1){ //重置计时器 } var i=0; var timer=setInterval(函数(){ var x=document.getElementById(“demo”).innerHTML=i; i=i+1; }, 500); } 您好 改变 重置
我想在单击时重置计时器。e、 g.如果setIntervaltime设置为5秒,并且已经过了3秒,那么如果点击重置按钮,它应该从5秒开始增益。如何做到这一点Javascript Onclick重置设置间隔 功能计时器(x){ 如果(x==1){ //重置计时器 } var i=0; var timer=setInterval(函数(){ var x=document.getElementById(“demo”).innerHTML=i; i=i+1; }, 500); } 您好 改变 重置,javascript,jquery,Javascript,Jquery,我想在单击时重置计时器。e、 g.如果setIntervaltime设置为5秒,并且已经过了3秒,那么如果点击重置按钮,它应该从5秒开始增益。如何做到这一点 将setTimeout的返回值保留在可以再次获取的位置(当前您将其存储在局部变量中,因此函数结束时它将消失) 调用clearTimeout(计时器) 再次调用setTimeout,使用所需的任何参数 将setTimeout的返回值保留在可以再次获取的位置(当前您将其存储在局部变量中,因此函数结束时它将消失) 调用clearTimeout(计
setTimeout
的返回值保留在可以再次获取的位置(当前您将其存储在局部变量中,因此函数结束时它将消失)clearTimeout(计时器)代码>
setTimeout
,使用所需的任何参数setTimeout
的返回值保留在可以再次获取的位置(当前您将其存储在局部变量中,因此函数结束时它将消失)clearTimeout(计时器)代码>
setTimeout
,使用所需的任何参数正如昆廷已经提到的,使用
clearInterval
来解决您的问题
将其包装在if.else..
语句中,如
<!DOCTYPE html>
<html>
<head>
<script>
function timer(x) {
if (x == 1) {
//reset timer
}
var i = 0;
var timer = setInterval(function() {
var x = document.getElementById("demo").innerHTML = i;
i = i + 1;
}, 500);
}
</script>
</head>
<body>
<p id="demo">hello</p>
<button type="button" onclick="timer(0)">change</button>
<button type="button" onclick="timer(1)">reset</button>
</body>
</html>
完整代码:
if(x == 1) {
clearTimeout(timeout);
}
else {
timeout = setInterval..... // otherwise even after resetting
// it will continue to increment the value
}
正如昆汀已经提到的,使用
clearInterval
来解决您的问题
将其包装在if.else..
语句中,如
<!DOCTYPE html>
<html>
<head>
<script>
function timer(x) {
if (x == 1) {
//reset timer
}
var i = 0;
var timer = setInterval(function() {
var x = document.getElementById("demo").innerHTML = i;
i = i + 1;
}, 500);
}
</script>
</head>
<body>
<p id="demo">hello</p>
<button type="button" onclick="timer(0)">change</button>
<button type="button" onclick="timer(1)">reset</button>
</body>
</html>
完整代码:
if(x == 1) {
clearTimeout(timeout);
}
else {
timeout = setInterval..... // otherwise even after resetting
// it will continue to increment the value
}
您不能使用相同的变量名来存储
setTimeout
和timer
函数的返回值。当您将timer
函数的作用域限定为一个名为onload
的函数时,您不能从onclick
属性调用函数。您能用小提琴来做吗?我对所有人来说都很陌生这是什么?你不能使用相同的变量名来存储setTimeout
和timer
函数的返回值。当你将timer
函数的作用域限定为一个名为onload
的函数时,你不能从onclick属性调用函数。你能用小提琴来做吗?我对这一切都很陌生?