Hi im new to javascript and html , I just want to know that how can I stop the third text field(txtS) at 60 to 0. I'm trying to make a clock ,so the seconds need to stop at 60 and minutes should start incrementing.**
嗨我是javascript和html的新手,我只是想知道如何在60到0停止第三个文本字段(txtS)。我正在尝试制作一个时钟,所以秒需要停在60和分钟应该开始递增。**
I'm only asking is to how to stop seconds at 60
我只想问如何在60秒停止秒
<html >
<head>
<title>clock</title>
<script language="javascript">
function fn_sample() {
document.frm.txtS.value = parseInt(document.frm.txtS.value) + 1;
var t1 = document.frm.txtS.value;
if(t1>=60){
t1 = 0;
}
window.setTimeout("fn_sample()", 1000);
}
</script>
</head>
<body>
<form name="frm">
<input type="text" name="txtH" value="0" size="2"/>
<input type="text" name="txtM" value="0" size="2"/>
<input type="text" name="txtS" value="0" size="2"/>
<br /><br />
<input type="button" value="Start" onclick="fn_sample();" />
</form>
</body>
1 个解决方案
#1
1
Just add else
after the close brace, to make setTimeout
depending on previous test.
只需在close括号后添加else,就可以根据之前的测试生成setTimeout。
(For demo, I've reduced timeout to 100
to wait 6 seconds instead of 60;-). Note for ensuring there in never more than 1 second for a full loop, you can't sleep 1 second between each operation! Prefer to use : window.setTimeout("fn_sample()",1000-(new Date()%1000));
. This will work until fn_sample()
execution time come over 1 second.
(对于演示,我将超时减少到100等待6秒而不是60 ;-)。注意确保完整循环的时间不超过1秒,每次操作之间不能睡1秒!更喜欢使用:window.setTimeout(“fn_sample()”,1000-(new Date()%1000));.这将一直有效,直到fn_sample()执行时间超过1秒。
function fn_sample() {
document.frm.txtS.value = parseInt(document.frm.txtS.value) + 1;
var t1 = document.frm.txtS.value;
if(t1>=60){
document.frm.txtS.value = 0;
} else
window.setTimeout("fn_sample()",
100-(new Date()%100));
}
<form name="frm">
<input type="text" name="txtH" value="0" size="2"/>
<input type="text" name="txtM" value="0" size="2"/>
<input type="text" name="txtS" value="0" size="2"/>
<br /><br />
<input type="button" value="Start" onclick="fn_sample();" />
</form>
#1
1
Just add else
after the close brace, to make setTimeout
depending on previous test.
只需在close括号后添加else,就可以根据之前的测试生成setTimeout。
(For demo, I've reduced timeout to 100
to wait 6 seconds instead of 60;-). Note for ensuring there in never more than 1 second for a full loop, you can't sleep 1 second between each operation! Prefer to use : window.setTimeout("fn_sample()",1000-(new Date()%1000));
. This will work until fn_sample()
execution time come over 1 second.
(对于演示,我将超时减少到100等待6秒而不是60 ;-)。注意确保完整循环的时间不超过1秒,每次操作之间不能睡1秒!更喜欢使用:window.setTimeout(“fn_sample()”,1000-(new Date()%1000));.这将一直有效,直到fn_sample()执行时间超过1秒。
function fn_sample() {
document.frm.txtS.value = parseInt(document.frm.txtS.value) + 1;
var t1 = document.frm.txtS.value;
if(t1>=60){
document.frm.txtS.value = 0;
} else
window.setTimeout("fn_sample()",
100-(new Date()%100));
}
<form name="frm">
<input type="text" name="txtH" value="0" size="2"/>
<input type="text" name="txtM" value="0" size="2"/>
<input type="text" name="txtS" value="0" size="2"/>
<br /><br />
<input type="button" value="Start" onclick="fn_sample();" />
</form>