怎么讓一個(gè)計(jì)時(shí)器可以不停地暫停和開(kāi)始呢
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
??
? ?function clock(){
? ? ? var time=new Date(); ? ?
? ? ? var atime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
? ? ? document.getElementById("cloc").value = atime;
? ?}
?var aa=setInterval("clock()",2000);
</script>
</head>
<body>
? <form>
? ? <input type="text" id="cloc" size="50" ?/>
? ? <input type="button" value="Stop" onclick="clearInterval(aa)" ? />
<input type="button" value="start" onclick="clock()" ? />
? </form>
</body>
</html>
這個(gè)計(jì)時(shí)器只能暫停一次,再點(diǎn)開(kāi)始就沒(méi)有了計(jì)時(shí)器的功能了
2016-09-18
看了一下各位的代碼,把我剛做的倒計(jì)時(shí)加了個(gè)開(kāi)始和停止。。。
?????分析原理:
定義一個(gè)變量裝時(shí)間函數(shù)“sh = setTimeout(setTime,500) ‘’; ???
網(wǎng)頁(yè)開(kāi)始時(shí)是開(kāi)始及時(shí)的,開(kāi)始(setTimeout)和停止(?clearInterval)應(yīng)在設(shè)置時(shí)間函數(shù)的外面去控制
2016-10-12
我做這題的時(shí)候也想設(shè)置一個(gè)開(kāi)始按鈕 ?我這么寫(xiě)自己測(cè)試是可以實(shí)現(xiàn)開(kāi)始跟暫停 但不知道有沒(méi)有什么其他問(wèn)題
2016-09-18
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
? var a;
function clock(){
? ? ? var time=new Date(); ? ?
? ? ? var atime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();//獲取具體時(shí)間
? ? ? document.getElementById("cloc").value = atime;//將獲取的時(shí)間在文本框內(nèi)顯示出來(lái)
}
function p(){
? a=setInterval("clock()",2000);//計(jì)時(shí)器
? ? }
? function q(){
? clearInterval(a);//清除計(jì)時(shí)器
? }
</script>
</head>
<body>
? <form>
? ? <input type="text" id="cloc" size="50" ?/>
? ? <input type="button" value="Stop" onclick="q()" ? />
<input type="button" value="Start" onclick="p()" ? />
? </form>
</body>
</html>
我把這個(gè)計(jì)時(shí)器重新寫(xiě)了下,點(diǎn)開(kāi)始時(shí)才開(kāi)始計(jì)時(shí)。
2016-09-18
<!DOCTYPE html>
<html>
?? ?<head>
?? ??? ?<meta charset="UTF-8">
?? ??? ?<title></title>
?? ??? ?<script type="text/javascript" language="JavaScript">
?? ??? ??? ?var sh;
?? ??? ??? ?window.onload = function(){
?? ??? ??? ??? ?setTime();
?? ??? ??? ?}
?? ??? ??? ?function checkTime(t){
?? ??? ??? ??? ?if(t<10){
?? ??? ??? ??? ??? ?t="0"+t;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return t;
?? ??? ??? ?}
?? ??? ??? ?function setTime(){
?? ??? ??? ??? ?var nowTime = new Date(); ??? ??? ??? ?? //當(dāng)前系統(tǒng)時(shí)間
?? ??? ??? ??? ?var endTime = new Date("2016/10/01,00:00:00"); // 結(jié)束時(shí)間
?? ??? ??? ??? ?var chaTime = parseInt((endTime.getTime() - nowTime.getTime())/1000);//獲取時(shí)間戳?? getTime()將時(shí)間戳處理成毫秒數(shù)
?? ??? ??? ??? ?
?? ??? ??? ??? ?var d = Math.floor(chaTime/(24*60*60));? //天
?? ??? ??? ??? ?var h =?? ?Math.floor((chaTime/(60*60))%24);//時(shí)
?? ??? ??? ??? ?var m = Math.floor((chaTime/(60))%60);?? //分
?? ??? ??? ??? ?var s = Math.floor(chaTime%60);?? ??? ??? ? //秒
?? ??? ??? ??? ??? ?m = checkTime(m);
?? ??? ??? ??? ??? ?s = checkTime(s);
?? ??? ??? ??? ?
?? ??? ??? ??? ?if(chaTime<=0){
?? ??? ??? ??? ??? ?document.getElementById("showTime").innerHTML="團(tuán)購(gòu)結(jié)束";?? ?
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?document.getElementById("showTime").innerHTML=
?? ??? ??? ??? ?"距離10月1號(hào)還剩"+d+"天"+h+"時(shí)"+m+"分"+s+"秒";
?? ??? ??? ??? ?clearInterval(sh);//清除計(jì)時(shí)器
?? ??? ??? ??? ?}
?? ??? ??? ??? ?sh=setTimeout(setTime,500); ?
?? ??? ??? ?}
?? ??? ??? ??? ?var start = function(){
?? ??? ??? ??? ?sh=setTimeout(setTime,500); ?? ?
?? ??? ??? ??? ?}
?? ??? ??? ??? ?var stop = function(){?? ?
?? ??? ??? ??? ??? ?clearInterval(sh); ??? ?
?? ??? ??? ??? ?} ?
?? ??? ?</script>
?? ?</head>
?? ?<body>
?? ??? ?<span id="showTime" style="color: red;"></span>
?? ??? ?<input type="button" value="停止倒計(jì)時(shí)"? onclick="stop()"/>
?? ??? ?<input type="button" value="開(kāi)始倒計(jì)時(shí)"? onclick="start()"/>
?? ?</body>
</html>
2016-09-18
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
? ?function clock(){
? ? ? var time=new Date(); ? ?
// ?document.write(time+"<br />");//獲取時(shí)間
? ? ? var atime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();//獲取具體時(shí)間
? ? ? document.getElementById("cloc").value = time;//將獲取的時(shí)間在文本框內(nèi)顯示出來(lái)
? ?}
? ?var aa=setInterval("clock()",2000);//計(jì)時(shí)器
? ?function s(){
? ? ?aa=setInterval("clock()",2000);//計(jì)時(shí)器
}
</script>
</head>
<body>
? <form>
? ? <input type="text" id="cloc" size="50" ?/>
? ? <input type="button" value="Stop" onclick="clearInterval(aa)" ? />
<input type="button" value="Start" onclick="s()" ? />
? </form>
</body>
</html>
貌似我把第九行注釋掉就沒(méi)問(wèn)題了,可以開(kāi)始和停止了
2016-09-18
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
? var panduan;
? ?function clock(){
? ? ? var time=new Date(); ? ?
? ? ? var atime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
? ? ? document.getElementById("cloc").value = atime;
? ?}
?panduan=setInterval("clock()",1000);
function start(){
? ? ?panduan=setInterval("clock()",1000);
}
function delet(){
clearInterval(panduan);
}
</script>
</head>
<body>
? <form>
? ? <input type="text" id="cloc" size="50" ?/>
? ? <input type="button" value="Stop" onclick="delet()" ? />
<input type="button" value="start" onclick="start()" ? />
? </form>
</body>
</html>
2016-09-18
額,我這邊連暫停都不行0.0
2016-09-18
把下面呢個(gè)按鈕添加為onclick=setInterval("clock()",2000);你試試