Solved

timers

Posted on 2010-11-07
8
278 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Expert Comment

by:JESii
Comment Utility
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
Comment Utility
Now how would you make a function that would stop the timer.  
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
It worked how would you have a label that would calculate the total time of each timer running
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

772 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

10 Experts available now in Live!

Get 1:1 Help Now