我在网上找了一个计时器,但我现在想加两个功能,一个是暂停计时(不是停止恢复成0,是暂停计时),第二个是从新计时,如何实现呢?麻烦大哥们帮改改,谢谢
<script language=JavaScript>
var timerID = null;
var timerRunning = false;var hours = 0
var minutes = 0
var seconds = 0function stopclock ()
{
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}function startclock () 
{
stopclock();
showtime();
}
function showtime () 
{
hours = 0
seconds = seconds+1
if (seconds >59)
{
seconds=1 
minutes = minutes+1;
}if (minutes > 59)
{
minutes=1
hours =hours+1
}var timeValue = hours+":"+minutes+":"+seconds; 
document.clock.thetime.value = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
</SCRIPT>
<body onload=startclock()>
<form name=clock >
<input name=thetime style="font-size: 9pt;color:#000000;border:0" size=12>
</form>
</body>

解决方案 »

  1.   

    <html>
    <head>
    <script>
    var timerID = null;
    var timerRunning = false;
    var paused = false;var hours = 0;
    var minutes = 0;
    var seconds = 0;function stopclock() {
        if (timerRunning) clearTimeout(timerID);
        timerRunning = false;
    }function startclock() {
    hours = minutes = seconds = 0;
    paused = false;
        stopclock();
        showtime();
    }function showtime() {
    if (paused) return;
        hours = 0
        seconds = seconds + 1
        if (seconds > 59) {
            seconds = 1
            minutes = minutes + 1;
        }    if (minutes > 59) {
            minutes = 1
            hours = hours + 1
        }    var timeValue = hours + ":" + minutes + ":" + seconds;
        document.clock.thetime.value = timeValue;
        timerID = setTimeout("showtime()", 1000);
        timerRunning = true;
    }function pauseclock() {
    paused = !paused;
    if (paused) {
    if (timerRunning) clearTimeout(timerID);
    timerRunning = false;
    } else {
    timerID = setTimeout("showtime()", 1000);
    timerRunning = true;
    }
    }
    </script>
    </head>
    <body onload="startclock()">
    <form name="clock">
    <input name="thetime" style="font-size:9pt;color:#000000;border:0" size="12"/>
    </form>
    <input type="button" value="暂停/运行" onclick="pauseclock()"/>
    <input type="button" value="重新开始" onclick="startclock()"/>
    </body>
    </html>
      

  2.   

    没错。就是clearTimeout这个方法。
      

  3.   

    下面是根据lz的代码修改的。个人建议用setInterval(),而不是setTimeout('fun()',t)。应为后者的实际执行时间是t+程序自身执行时间。
    <script type="text/javascript">
    var timerID = null;
    var timerRunning = false;var hours = 0
    var minutes = 0
    var seconds = 0function stopclock ()
    {
    if(timerRunning){
    clearTimeout(timerID);
    timerRunning = false;
    }else{
    timerRunning = true;
    }
    }function startclock () 
    {
    stopclock();
    showtime();
    }
    function showtime () 
    {
    if(!timerRunning) return false;
    seconds = seconds+1;
    if (seconds >59)
    {
    seconds=0;
    minutes = minutes+1;
    } if (minutes > 59)
    {
    minutes=0;
    hours =hours+1
    }
    var timeValue = hours+":"+minutes+":"+seconds; 
    document.clock.thetime.value = timeValue;
    timerID = setTimeout("showtime()",1000);
    }
    </script>