Link to home
Start Free TrialLog in
Avatar of stargateatlantis
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(function() {                  
            timer1 = setInterval($.do_time('hr1','min1','sec1'), 1000);
        });
      
      $("#btn2Start").click(function() {                  
            timer2 = setInterval($.do_time('hr2','min2','sec2'), 1000);
        });

      $("#btn3Start").click(function() {                  
            timer3 = setInterval($.do_time('hr3','min3','sec3'), 1000);
        });


//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);

Open in new window

Avatar of hosneylk
hosneylk
Flag of Singapore image

try using val() instead of html()

e.g.
$("#"+hrName).val("0".substring(hour >= 10) + hour);

it might work.. just an idea
Avatar of leakim971
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','sec1')", 1000);

http://www.w3schools.com/jsref/met_win_setinterval.asp
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Avatar of stargateatlantis
stargateatlantis

ASKER

Now how would you make a function that would stop the timer.  
Use clearInterval(id_of_setinterval) : 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) });
It worked how would you have a label that would calculate the total time of each timer running