stargateatlantis
asked on
timers
I created the following function and calling it like this but the problem is i have to keep hitting the start button for the text area to change seconds. I want to be able to click on start and it will count automatically but it isn't working now. Why is that?
$("#btn1Start").click(func tion() {
timer1 = setInterval($.do_time('hr1 ','min1',' sec1'), 1000);
});
$("#btn2Start").click(func tion() {
timer2 = setInterval($.do_time('hr2 ','min2',' sec2'), 1000);
});
$("#btn3Start").click(func tion() {
timer3 = setInterval($.do_time('hr3 ','min3',' sec3'), 1000);
});
$("#btn1Start").click(func
timer1 = setInterval($.do_time('hr1
});
$("#btn2Start").click(func
timer2 = setInterval($.do_time('hr2
});
$("#btn3Start").click(func
timer3 = setInterval($.do_time('hr3
});
//Timer JQuery function
(function($)
{
$.do_time = function(hrName,minName,secNam) {
// parseInt() doesn't work here...
hour = parseFloat($("#"+hrName).text());
minute = parseFloat($("#"+minName).text());
second = parseFloat($("#"+secNam).text());
second++;
if(second > 59) {
second = 0;
minute = minute + 1;
}
if(minute > 59) {
minute = 0;
hour = hour + 1;
}
$("#"+hrName).html("0".substring(hour >= 10) + hour);
$("#"+minName).html("0".substring(minute >= 10) + minute);
$("#"+secNam).html("0".substring(second >= 10) + second);
}//End Doo time function
})(jQuery);
put the function to execute as a string not directly.
so replace :
timer1 = setInterval($.do_time('hr1 ','min1',' sec1'), 1000);
by :
timer1 = setInterval("$.do_time('hr1','min1','se c1')", 1000);
http://www.w3schools.com/jsref/met_win_setinterval.asp
so replace :
timer1 = setInterval($.do_time('hr1
by :
timer1 = setInterval("$.do_time('hr1','min1','se
http://www.w3schools.com/jsref/met_win_setinterval.asp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
FYI, there's a very simple, working example of this in the W3CSchools site: http://www.w3schools.com/js/tryit.asp?filename=tryjs_setinterval. That may help you understand the moving parts?
ASKER
Now how would you make a function that would stop the timer.
Use clearInterval(id_of_setint erval) : http://www.w3schools.com/jsref/met_win_clearinterval.asp
$("#myStopButton1").click( function() { if(timer1) clearInterval(timer1) });
$("#myStopButton2").click( function() { if(timer2) clearInterval(timer2) });
$("#myStopButton3").click( function() { if(timer3) clearInterval(timer3) });
$("#myStopButton1").click(
$("#myStopButton2").click(
$("#myStopButton3").click(
ASKER
It worked how would you have a label that would calculate the total time of each timer running
e.g.
$("#"+hrName).val("0".subs
it might work.. just an idea