Solved

timers

Posted on 2010-11-07
8
279 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:stargateatlantis
8 Comments
 
LVL 10

Expert Comment

by:hosneylk
ID: 34081480
try using val() instead of html()

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

it might work.. just an idea
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34081529
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
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 34081536
Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
    $("#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);

</script>
</head>
<body>
<table width="160">
<tr><td id="hr1">00</td><td id="min1">00</td><td id="sec1">00</td><td><input type="button" id="btn1Start" value="button1" /></td></tr>
<tr><td id="hr2">00</td><td id="min2">00</td><td id="sec2">00</td><td><input type="button" id="btn2Start" value="button2" /></td></tr>
<tr><td id="hr3">00</td><td id="min3">00</td><td id="sec3">00</td><td><input type="button" id="btn3Start" value="button3" /></td></tr>
</table>
</body>
</html>

Open in new window

0
 
LVL 14

Assisted Solution

by:sam2912
sam2912 earned 250 total points
ID: 34082133
BTW, parseInt() does work, but because it is parsing a string with a leading zero i.e. "08", it treats it like an octal number. What you need to do is tell it to parse in base 10 using the less known second parameter if parseInt().

Why not also combine the H:M:S into one field?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript">
$(document).ready(function () {
	$("#btn1Start").click(function () {
		timer1 = setInterval("$.do_time('timer1')", 1000);
		$(this).attr("disabled", true);
	});
	$("#btn2Start").click(function () {
		timer2 = setInterval("$.do_time('timer2')", 1000);
		$(this).attr("disabled", true);
	});
	$("#btn3Start").click(function () {
		timer3 = setInterval("$.do_time('timer3')", 1000);
		$(this).attr("disabled", true);
	});
});
//Timer JQuery function
(function ($) {
	$.do_time = function(timerID) {
		
		var time = ($("#" + timerID).html()).split(':');
		var hour = parseInt(time[0], 10);
		var minute = parseInt(time[1], 10);
		var second = parseInt(time[2], 10) + 1;
		
		if (second > 59) {
			second = 0;
			minute++;
		}
		if (minute > 59) {
			minute = 0;
			hour++;
		}
		$("#" + timerID).html((hour>9?hour:'0'+hour) +':'+ (minute>9?minute:'0'+minute) +':'+ (second>9?second:'0'+second));
	} //End Doo time function
})(jQuery);
</script>
</head>
<body>
<table width="160">
   <tr>
      <td id="timer1">00:00:00</td>
      <td><input type="button" id="btn1Start" value="button1" /></td>
   </tr>
   <tr>
      <td id="timer2">00:00:00</td>
      <td><input type="button" id="btn2Start" value="button2" /></td>
   </tr>
   <tr>
      <td id="timer3">00:00:00</td>
      <td><input type="button" id="btn3Start" value="button3" /></td>
   </tr>
</table>
</body>
</html>

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

Expert Comment

by:JESii
ID: 34083444
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?
0
 

Author Comment

by:stargateatlantis
ID: 34083502
Now how would you make a function that would stop the timer.  
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34083524
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) });
0
 

Author Comment

by:stargateatlantis
ID: 34083825
It worked how would you have a label that would calculate the total time of each timer running
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript export function not working in IE 15 37
Enter key on html form page 11 48
Change javascript css 1 20
insert text field data into html script 13 25
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now