Solved

timers

Posted on 2010-11-07
8
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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
 
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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

622 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