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

jQueryJavaScript

Avatar of undefined
Last Comment
stargateatlantis
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
leakim971
Flag of Guadeloupe image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Samuel Liew
Samuel Liew
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of JESii
JESii
Flag of United States of America image

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.  
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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) });
Avatar of stargateatlantis

ASKER

It worked how would you have a label that would calculate the total time of each timer running
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo