Solved

expire time

Posted on 2010-11-10
9
388 Views
Last Modified: 2012-05-10
I have the following code here but the question is I want the function $.do_time pass a value called expire time.  So when button1Start and the expire time is getting close the color of the font would change.  

$("#btn1Start").click(function () {
            timer1 = setInterval("$.do_time('timer1',"00:10:00","red")", 1000);
            $(this).attr("disabled", true);
      });
<!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);
	});
	totaltimer = setInterval("$.displayTotal('totaltimer')", 1000);
});
//Timer JQuery function
(function ($) {
   totaltime = "00:00:00";
	$.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;
		
		var ttime = totaltime.split(':');
		var thour = parseInt(ttime[0], 10);
		var tminute = parseInt(ttime[1], 10);
		var tsecond = parseInt(ttime[2], 10) + 1;
		
		if (second > 59) {
			second = 0;
			minute++;
		}
		if (minute > 59) {
			minute = 0;
			hour++;
		}
		if (tsecond > 59) {
			tsecond = 0;
			tminute++;
		}
		if (tminute > 59) {
			tminute = 0;
			thour++;
		}
		
		time = (hour>9?hour:'0'+hour) +':'+ (minute>9?minute:'0'+minute) +':'+ (second>9?second:'0'+second);
		$("#" + timerID).html(time);
		totaltime = "" + (thour>9?thour:'0'+thour) +':'+ (tminute>9?tminute:'0'+tminute) +':'+ (tsecond>9?tsecond:'0'+tsecond);
	} //End Doo time function
	$.displayTotal = function(labelID) {
		$("#" + labelID).html(totaltime);
	}
})(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>
   <tr>
      <td id="totaltimer">00:00:00</td>
      <td></td>
   </tr>
</table>
</body>
</html>

Open in new window

0
Comment
Question by:stargateatlantis
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 12

Expert Comment

by:JESii
ID: 34106482
Given the code you've already got, you probably know how to add the parameters to the call and the function.

Javascript recognizes dates, but not times without dates, so you may have to convert back and forth to do the comparison... something like this code might help you get started

Then you'll have to set your attribute after the comparison "matches"
// converting time string to a date:
// from http://www.webdeveloper.com/forum/showthread.php?t=123460
var a = "10:15"
var b = toDate(a,"h:m")
alert(b);
function toDate(dStr,format) {
	var now = new Date();
	if (format == "h:m") {
 		now.setHours(dStr.substr(0,dStr.indexOf(":")));
 		now.setMinutes(dStr.substr(dStr.indexOf(":")+1));
 		now.setSeconds(0);
 		return now;
	}else 
		return "Invalid Format";
}

// changing color attribute
$(timerID).attr({ style: "color: red;" } );

Open in new window

0
 

Author Comment

by:stargateatlantis
ID: 34106922
I don't need dates at all.  I just set the expire time to then if the timer ticker reaches that expired time then the text will change red.
0
 
LVL 1

Assisted Solution

by:ikbentomas
ikbentomas earned 333 total points
ID: 34110849
I took your script and added a few lines and i think this is what you are looking for.
<script language="javascript">
$(document).ready(function () {
	$("#btn1Start").click(function () {
		timer1 = setInterval("$.do_time('timer1','00:00:10','red')", 1000);
//the previous line was edited, watch which quotes you use
		$(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);
	});
	totaltimer = setInterval("$.displayTotal('totaltimer')", 1000);
});
//Timer JQuery function
(function ($) {
   totaltime = "00:00:00";
	$.do_time = function(timerID,exptime,color) {
		//the passed arguments are added
		var time = ($("#" + timerID).html()).split(':');
		var hour = parseInt(time[0], 10);
		var minute = parseInt(time[1], 10);
		var second = parseInt(time[2], 10) + 1;
		
		var ttime = totaltime.split(':');
		var thour = parseInt(ttime[0], 10);
		var tminute = parseInt(ttime[1], 10);
		var tsecond = parseInt(ttime[2], 10) + 1;
		
		if (second > 59) {
			second = 0;
			minute++;
		}
		if (minute > 59) {
			minute = 0;
			hour++;
		}
		if (tsecond > 59) {
			tsecond = 0;
			tminute++;
		}
		if (tminute > 59) {
			tminute = 0;
			thour++;
		}
		
		var temp = exptime.split(':');
		var expire = [];
		expire[0] = parseInt(temp[0],10);
		expire[1] = parseInt(temp[1],10);
		expire[2] = parseInt(temp[2],10);
		if( expire[0] == hour && expire[1] == minute && expire[2] == second){
			$('#' + timerID).css('color',color);
		}
//this block of code is new, it can be smaller if optimized but this way its the easiest to understand.		

		time = (hour>9?hour:'0'+hour) +':'+ (minute>9?minute:'0'+minute) +':'+ (second>9?second:'0'+second);
		$("#" + timerID).html(time);
		totaltime = "" + (thour>9?thour:'0'+thour) +':'+ (tminute>9?tminute:'0'+tminute) +':'+ (tsecond>9?tsecond:'0'+tsecond);
	} //End Doo time function
	
	$.displayTotal = function(labelID) {
		$("#" + labelID).html(totaltime);
	}
})(jQuery);
</script>

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:sam2912
ID: 34114709
This is a double post. Check http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_26607756.html

BTW, the function should check if the parameter "exptime" exists, because it could be optional.
<!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', '00:00:05')", 1000);
                $(this).attr("disabled", true);
        });
        $("#btn2Start").click(function () {
                timer2 = setInterval("$.do_time('timer2', '00:00:10')", 1000);
                $(this).attr("disabled", true);
        });
        $("#btn3Start").click(function () {
                timer3 = setInterval("$.do_time('timer3')", 1000);
                $(this).attr("disabled", true);
        });
        totaltimer = setInterval("$.displayTotal('totaltimer')", 1000);
});
//Timer JQuery function
(function ($) {
   totaltime = "00:00:00";
        $.do_time = function(timerID, expireTime) {
                
                var time = ($("#" + timerID).html()).split(':');
                var hour = parseInt(time[0], 10);
                var minute = parseInt(time[1], 10);
                var second = parseInt(time[2], 10) + 1;
                
                var ttime = totaltime.split(':');
                var thour = parseInt(ttime[0], 10);
                var tminute = parseInt(ttime[1], 10);
                var tsecond = parseInt(ttime[2], 10) + 1;
                
                if (second > 59) {
                        second = 0;
                        minute++;
                }
                if (minute > 59) {
                        minute = 0;
                        hour++;
                }
                if (tsecond > 59) {
                        tsecond = 0;
                        tminute++;
                }
                if (tminute > 59) {
                        tminute = 0;
                        thour++;
                }
                
                if(expireTime) {
                        var expired = false;
                        var etime = expireTime.split(':');
                        var ehour = parseInt(etime[0], 10);
                        var eminute = parseInt(etime[1], 10);
                        var esecond = parseInt(etime[2], 10);
                        
                        if(ehour<hour) {
                                expired = true;
                        }
                        else if(ehour==hour) {
                                if(eminute<minute) {
                                        expired = true;
                                }
                                else if(eminute==minute) {
                                        if(esecond<=second) {
                                                expired = true;
                                        }
                                }
                        }
                        
                        if(expired) {
                                $("#" + timerID).css("background-color", "red");
                        }
                }
                
                time = (hour>9?hour:'0'+hour) +':'+ (minute>9?minute:'0'+minute) +':'+ (second>9?second:'0'+second);
                $("#" + timerID).html(time);
                totaltime = "" + (thour>9?thour:'0'+thour) +':'+ (tminute>9?tminute:'0'+tminute) +':'+ (tsecond>9?tsecond:'0'+tsecond);
        } //End Doo time function
        $.displayTotal = function(labelID) {
                $("#" + labelID).html(totaltime);
        }
})(jQuery);
</script>
</head>
<body>
<table width="300">
   <tr>
      <td id="timer1">00:00:00</td>
      <td><input type="button" id="btn1Start" value="button1" /> (Expires in 5 seconds)</td>
   </tr>
   <tr>
      <td id="timer2">00:00:00</td>
      <td><input type="button" id="btn2Start" value="button2" /> (Expires in 10 seconds)</td>
   </tr>
   <tr>
      <td id="timer3">00:00:00</td>
      <td><input type="button" id="btn3Start" value="button3" /> (Does not expire)</td>
   </tr>
   <tr>
      <td id="totaltimer">00:00:00</td>
      <td></td>
   </tr>
</table>
</body>
</html>

Open in new window

0
 
LVL 1

Expert Comment

by:ikbentomas
ID: 34118518
@sam2912 you are right about checking the existence of the exptime variable.
but I do find my solution more elegant since it doesn't apply the color every second after exptime.

A better solution, in my opinion, would be this:
if(exptime){
	var temp = exptime.split(':');
	var expire = [];
	expire[0] = parseInt(temp[0],10);
	expire[1] = parseInt(temp[1],10);
	expire[2] = parseInt(temp[2],10);
	if( expire[0] == hour && expire[1] == minute && expire[2] == second){
		if(color){
			$('#' + timerID).css('color',color);
		} else {
			$('#' + timerID).css('color',"red");
		}
	}
}

Open in new window

0
 

Author Comment

by:stargateatlantis
ID: 34125773
ikbentomas this is true but lets say you had more then 3 buttons the expire time solution only works for 3 buttons .  Correct me if i am wrong.  
0
 
LVL 14

Accepted Solution

by:
sam2912 earned 167 total points
ID: 34126243
As long as the value is passed into the function, it can work for as many timers as you want.
0
 
LVL 1

Assisted Solution

by:ikbentomas
ikbentomas earned 333 total points
ID: 34128261
Hi stargateatlantis,
I guess you are confused by this part:
var expire = [];
expire[0] = parseInt(temp[0],10);
expire[1] = parseInt(temp[1],10);
expire[2] = parseInt(temp[2],10);

perhaps the exact same solution with other variables makes more sense to you:
if(exptime){
	var splittime = exptime.split(':'); //put 00, 00 and 10 in the array splittime
	exp_hour   = parseInt(splittime[0],10); // converts "00" to 0
	exp_minute = parseInt(splittime[1],10); // converts "00" to 0 
	exp_second = parseInt(splittime[2],10); // converts "10" to 10
	if( exp_hour == hour && exp_minute == minute && exp_second == second){
		if(color){
			$('#' + timerID).css('color',color);
		} else {
			$('#' + timerID).css('color',"red");
		}
	}
}

Open in new window

0
 

Author Comment

by:stargateatlantis
ID: 34128930
Now it makes scene thanks...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

749 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