/ Published in: JavaScript
URL: http://www.mechanicmatt.com
Expand |
Embed | Plain Text
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>OOP JS Timer; by: Matt Ford</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script> <script type="text/javascript"> <!-- var timer = { clockID: "currentTime", timerStartID: "startTime", timerTotalID: "timerTotal", numSeconds: 0, timerIsActive: false, getCTime: function() { mydate = new Date(); hours = mydate.getHours(); minutes = mydate.getMinutes(); seconds = mydate.getSeconds(); dn = "AM"; if (hours >= 12) { dn = "PM"; } else {} if (hours > 12){ hours = hours - 12; } else {} if (hours == 0) { hours = 12; } else {} if (minutes <= 9) { minutes = "0" + minutes; } else {} if (seconds <= 9) { seconds = "0" + seconds; } else {} cdate = this.leadingZeros(hours, 2, "0") + ":" + this.leadingZeros(minutes, 2, "0") + ":" + this.leadingZeros(seconds, 2, "0") + " " + dn; if (this.timerIsActive === true) { this.updateTimer(); } else {} return cdate; }, updateCTime: function() { if (this.clockID != "") { $(this.clockID).innerHTML = this.getCTime(); setTimeout("timer.updateCTime()", 1000); } else {} }, startTime: function() { $(this.timerStartID).innerHTML = this.getCTime(); }, startTimer: function() { this.stopClear(); this.timerIsActive = true; this.startTime(); }, pauseTimer: function() { if (this.timerIsActive === false) { this.timerIsActive = true; } else { this.timerIsActive = false; } }, stopClear: function() { this.timerIsActive = false; $(this.timerStartID).innerHTML = "n/a"; $(this.timerTotalID).innerHTML = "n/a hours<br />n/a minutes<br />n/a seconds"; this.numSeconds = 0; }, updateTimer: function() { timeParts = this.convertSeconds(); $(this.timerTotalID).innerHTML = this.leadingZeros(timeParts[0], 2, "0") + " hours <br />" + this.leadingZeros(timeParts[1], 2, "0") + " minutes<br />" + this.leadingZeros(timeParts[2], 2, "0") + " seconds"; ++this.numSeconds; }, convertSeconds: function() { secondsInHour = 3600; secondsInMinute = 60; Hours = Math.floor(this.numSeconds / secondsInHour); Minutes = Math.floor((this.numSeconds - (Hours * secondsInHour)) / secondsInMinute); Seconds = (this.numSeconds - (Hours * secondsInHour) - (Minutes * secondsInMinute)); return [Hours, Minutes, Seconds]; }, initCode: function() { this.updateCTime(); this.stopClear(); }, leadingZeros: function(num, totalChars, padWith) { num = num + ""; padWith = (padWith) ? padWith : "0"; if (num.length < totalChars) { while (num.length < totalChars) { num = padWith + num; } } else {} if (num.length > totalChars) { //if padWith was a multiple character string and num was overpadded num = num.substring((num.length - totalChars), totalChars); } else {} return num; } }; --> </script> <style type="text/css"> <!-- .cTimeText { color: #666666; font-size: 17px; font-weight: bold; text-align: center; } .time { font-size: 40px; font-weight: bold; text-align: center; } .controls { text-align: center; } .CTB { font-size: 16px; font-weight: bold; } .inner { padding: 10px; } .header { text-align: center; background-color: #355F91; margin-bottom: 10px; } .copyright { font-size: 8px; color: #CCCCCC; text-align: center; position: absolute; bottom: 0px; left: 0px; width: 100%; } .spacer { padding-top: 10px; } .container { width: 400px; border: 1px solid #000000; } .controls img { cursor: pointer; } --> </style> </head> <body onload="timer.initCode()"> <table align="center" cellspacing="0" class="container"> <tr> <td valign="top" nowrap="nowrap"> <div class="inner"> <div class="cTimeText">current time:</div> <div class="time" id="currentTime">00:00:00 PM</div> <div class="spacer"> </div> <div class="cTimeText">start time:</div> <div class="time" id="startTime">n/a</div> <div class="spacer"> </div> <div class="cTimeText">timer total:</div> <div class="time" id="timerTotal"> </div> <div class="spacer"> </div> <div class="controls"><img src="images/start.jpg" alt="" width="106" height="47" onclick="timer.startTimer();" /><br /> <img src="images/pause.jpg" alt="" width="106" height="47" onclick="timer.pauseTimer();" /><br /> <img src="images/stop.jpg" alt="" width="106" height="47" onclick="timer.stopClear();" /></div> </div></td> </tr> </table> <div class="copyright">Copyright © Matt Ford. All Rights Reserved.</div> </body> </html>
You need to login to post a comment.
