在JavaScript中,如果用户答对了,如何清除区间?
In JavaScript, how do I clear the interval if the user gets the answer right?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Clear Interval If</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/5.4.0/bootbox.js"></script>
</head>
<body>
<div id="main">
<button onClick="start()">Click</button>
</div>
<script>
function start(){
alert('5 seconds')
setTimeout(function(){
bootbox.prompt({
className:'questionOne',
title: "type hspeaker",
centerVertical: true,
callback: function(result){
if(result===null){alert("try again?");bootbox.hideAll();location.reload()}
var bashed = /hspeaker/i;
if(result.match(bashed)===null){alert('incorrect:Try again?');location.reload()}
var result01 = result.match(bashed);
if(result01[0]==='hspeaker'){alert('correct:'+result01);}
}});
setTimeout(function(){alert("out of time")},7000);},5000);}
</script>
</body>
</html>
如果用户在 7 秒内没有给出答案,我只希望第二个超时函数 运行。我尝试使用 clearTimeout(),但它无法识别我的 if 语句,并且每次都只是 运行ning,无论用户是否及时回答。我尝试在
中使用它
if(result01[0]==='hspeaker'){alert('correct:'+result01);}
提前致谢。
将其存储在变量中并调用
clearTimeout(variableName)
有条件
const timeout = setTimeout(()=> console.log("timeout"),3000);
function stop(){
clearTimeout(timeout);
console.log("timeout stopped");
}
<button onclick="stop()">Stop Timeout</button>
function start(){
alert('5 seconds')
setTimeout(function(){
bootbox.prompt({
className:'questionOne',
title: "type hspeaker",
centerVertical: true,
callback: function(result){
if(result===null){
alert("try again?");
bootbox.hideAll();
clearTimeout(outOfTime);
location.reload()
}
var bashed = /hspeaker/i;
if(result.match(bashed)===null){
alert('incorrect:Try again?');
clearTimeout(outOfTime);
location.reload();
}
var result01 = result.match(bashed);
if(result01[0]==='hspeaker'){
alert('correct:'+result01);
clearTimeout(outOfTime);
}
}
});
const outOfTime = setTimeout(function(){
alert("out of time")
},7000);
},5000);
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Clear Interval If</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/5.4.0/bootbox.js"></script>
</head>
<body>
<div id="main">
<button onClick="start()">Click</button>
</div>
<script>
function start(){
alert('5 seconds')
setTimeout(function(){
bootbox.prompt({
className:'questionOne',
title: "type hspeaker",
centerVertical: true,
callback: function(result){
if(result===null){alert("try again?");bootbox.hideAll();location.reload()}
var bashed = /hspeaker/i;
if(result.match(bashed)===null){alert('incorrect:Try again?');location.reload()}
var result01 = result.match(bashed);
if(result01[0]==='hspeaker'){alert('correct:'+result01);}
}});
setTimeout(function(){alert("out of time")},7000);},5000);}
</script>
</body>
</html>
如果用户在 7 秒内没有给出答案,我只希望第二个超时函数 运行。我尝试使用 clearTimeout(),但它无法识别我的 if 语句,并且每次都只是 运行ning,无论用户是否及时回答。我尝试在
中使用它if(result01[0]==='hspeaker'){alert('correct:'+result01);}
提前致谢。
将其存储在变量中并调用
clearTimeout(variableName)
有条件
const timeout = setTimeout(()=> console.log("timeout"),3000);
function stop(){
clearTimeout(timeout);
console.log("timeout stopped");
}
<button onclick="stop()">Stop Timeout</button>
function start(){
alert('5 seconds')
setTimeout(function(){
bootbox.prompt({
className:'questionOne',
title: "type hspeaker",
centerVertical: true,
callback: function(result){
if(result===null){
alert("try again?");
bootbox.hideAll();
clearTimeout(outOfTime);
location.reload()
}
var bashed = /hspeaker/i;
if(result.match(bashed)===null){
alert('incorrect:Try again?');
clearTimeout(outOfTime);
location.reload();
}
var result01 = result.match(bashed);
if(result01[0]==='hspeaker'){
alert('correct:'+result01);
clearTimeout(outOfTime);
}
}
});
const outOfTime = setTimeout(function(){
alert("out of time")
},7000);
},5000);
}