作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望创建一个煮蛋计时器应用程序,用户可以通过各种表单选择标签传递值。通过这样做,他们设置了计时器,当我尝试这样做时,我得到了一个 NaN 错误。在下面,我有我目前正在使用的 javascript 和一些 HTML,但不是全部。
<html>
<head>
<script language = "javascript" type = "text/javascript">
var minutes
var miuntes=setInterval(timer, 1000); //1000 will run it every 1 second
function timer(m)
{
minutes = m;
minutes=minutes-1;
if (minutes <= 0)
{
clearInterval(minutes);
//counter ended, do something here
return;
}
document.getElementById("txt").innerHTML=minutes + "minutes"; // watch for spelling
}
function checktimer()
{
var checkbox1 = form.preference.value;
var checkbox2 = form.eggsize.value;
var checkbox3 = form.suacepansize.value;
if(checkbox1 == 1 && checkbox2 == 1 && checkbox3 == 1)
{
m = 3;
s = 0;
}
timer(m);
}
</script>
</head>
<body>
<div id = "txt"> </div>
<form id="form" onclick ="checktimer()">
<div class = "question">
<div class="circle"> <h2 class = "whiteh2"> 1 </h2></div>
<div class="question-text"><h6>How do you like your egg? </h6> </div>
<select id="prefernce" name ="preference">
<option value="1">Soft</option>
<option value="2">Medium</option>
<option value="3">Hard</option>
</select>
</div> <!--Ends the question div-->
<div class = "question">
<div class="circle"> <h2 class = "whiteh2"> 2 </h2></div>
<div class="question-text"><h6>What size is your egg? </h6> </div>
<select id="eggsize" name = "eggsize">
<option value="1">Small</option>
<option value="2">Medium</option>
<option value="3">Large</option>
</select>
</div> <!--Ends the question div-->
<div class = "question">
<div class="circle"> <h2 class = "whiteh2"> 3 </h2></div>
<div class="question-text"><h6>What size is the saucepan? </h6> </div>
<select id="saucepansize" name = "suacepansize">
<option value="1">Small</option>
<option value="2">Medium</option>
<option value="3">Large</option>
</select>
</div> <!--Ends the question div-->
<div class = "question">
<input type="submit" id="button" name="submit" value = "Go" onclick="checktimer()"/> <br/>
</div>
</form>
</div> <!--Ends the main form div-->
</body>
</html>
最佳答案
我做了一些改变。
onclick
事件移动到 onsubmit
事件,并从该函数返回 false 以停止提交表单。m
参数,因为您有全局分钟变量注意:目前,它以每秒一分钟的速度倒计时 - 您需要将分钟乘以 60 并以秒为单位倒计时,或者每分钟只触发一次间隔。
完整调整后的脚本是:
<html>
<head>
<script type="text/javascript">
var minutes = 0;
var interval;
function timer() {
minutes = minutes - 1;
document.getElementById("txt").innerHTML= minutes + " minutes"; // watch for spelling
if (minutes <= 0) {
//counter ended, do something here
window.clearInterval(interval);
return;
}
}
function checktimer() {
var checkbox1 = form.preference.value;
var checkbox2 = form.eggsize.value;
var checkbox3 = form.suacepansize.value;
if(checkbox1 == 1 && checkbox2 == 1 && checkbox3 == 1) {
minutes = 3;
s = 0;
}
interval = setInterval(timer, 1000);
return false;
}
</script>
</head>
<body>
<div id = "txt"> </div>
<form id="form" onsubmit="return checktimer()">
<div class = "question">
<div class="circle"> <h2 class = "whiteh2"> 1 </h2></div>
<div class="question-text"><h6>How do you like your egg? </h6> </div>
<select id="prefernce" name ="preference">
<option value="1">Soft</option>
<option value="2">Medium</option>
<option value="3">Hard</option>
</select>
</div> <!--Ends the question div-->
<div class = "question">
<div class="circle"> <h2 class = "whiteh2"> 2 </h2></div>
<div class="question-text"><h6>What size is your egg? </h6> </div>
<select id="eggsize" name = "eggsize">
<option value="1">Small</option>
<option value="2">Medium</option>
<option value="3">Large</option>
</select>
</div> <!--Ends the question div-->
<div class = "question">
<div class="circle"> <h2 class = "whiteh2"> 3 </h2></div>
<div class="question-text"><h6>What size is the saucepan? </h6> </div>
<select id="saucepansize" name = "suacepansize">
<option value="1">Small</option>
<option value="2">Medium</option>
<option value="3">Large</option>
</select>
</div> <!--Ends the question div-->
<div class = "question">
<input type="submit" id="button" name="submit" value = "Go"/> <br/>
</div>
</form>
</div> <!--Ends the main form div-->
</body>
</html>
关于javascript - 煮蛋计时器 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14022640/
我是一名优秀的程序员,十分优秀!