0
我試圖通過使用錨tag.I已經成功完成用多個秒錶計時器,但我不能夠添加功能,例如,如果我點擊第一個計時器,它會從零,當我點擊第二個啓動定時器,第一定時器的值將是零和第二個將來自zero.I啓動定時器我給我下面的工作代碼:如何重置秒錶的多次停止監視時間?
JS:
var digit=0;
var hrs = 0;
var min=0;
var time;
var timer_is_on=0;
var id;
function timer(id){
//[Old] - this should be placed after you increment digit
// document.getElementById("secs").innerHTML = digit
//alert("I am testing value"+id);
//[New]
// get the numerical value for seconds,minutes,hours
digit = parseInt(document.getElementById("secs"+id).innerHTML);
min = parseInt(document.getElementById("mins"+id).innerHTML);
hrs = parseInt(document.getElementById("hrs"+id).innerHTML);
// increment;
digit=digit+1;
if(digit>"59"){
min = parseInt(min)+1;
// why is this code here
var count = min.toString().length;
digit=0;
}
// [old] checking if second is greater than 59 and incrementing hours
// if(digit>"59"){
// [new] check if minute is greater than 59
if(min > "59"){
hrs=parseInt(hrs)+1;
digit=0;
min=0; // minute should be reset as well
}
// set the values after all processing is done
document.getElementById("secs"+id).innerHTML = format(digit);
document.getElementById("mins"+id).innerHTML= format(min);
document.getElementById("hrs"+id).innerHTML=format(hrs);
}
function activate(id){
if(!timer_is_on){
timer_is_on=1;
// time = setTimeout("timer()",1000) will only call it once , use setInterval instead
time=setInterval("timer("+id+")",1000);
}
else {
timer_is_on=0;
clearInterval(time); // clear the timer when the user presses the button again and reset timer_is_on
}
return id;
}
// left pad zero if it is less than 9
function format(time){
if(time > 9)
return time;
else return "0"+time;
}
而且我使用的HTML代碼是:
<a href="#" onclick="activate(1)">Click here to start the timer</a>
<span id="hrs1" >00</span>:<span id="mins1" >00</span>:<span id="secs1">00</span></strong>
<br/>
<a href="#" onclick="activate(2)">Click here to start the timer</a>
<span id="hrs2" >00</span>:<span id="mins2" >00</span>:<span id="secs2">00</span>
這是我的工作ING JS提琴演示:http://jsfiddle.net/EPtFW/1/
我憑着瞭解code.I正在使用激活功能,但我通過一個參數來區分。 – alamin8031
功能激活(ID){ \t //Stopping...running計時器 \t如果(timerMap [ID]){ \t \t clearInterval(timerMap [ID]); \t} \t \t \t \t \t \t //啓動計時器... \t timerMap [ID] =的setInterval( 「計時器(」 + ID + 「)」,1000); \t \t \t \t \t \t return id; } – MRaja
這裏id應該是唯一的,所以兩個定時器必須有不同的id。 – MRaja