Clocking off

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Clocking off

Post by agibsonsw »

Hello.

I created a clock for my page using JavaScript - code below. Initially it created a new Date() object each time and then created vars to build the date and time format. This seemed an unnecessary overhead and I'm trying to get it to run quickly/efficiently - (I don't mean to 'run fast' :laugh: ).

So I'm just creating the Date() object once and adding 3 seconds to it each time - of course, I'm only updating it every three seconds as well. Is there anything I should be aware of with this method? Will the clock suddenly be wrong at, say, the end of the month? It's hard for me to tell if there might be a problem :scratch:

Code: Select all

Date.prototype.getMonthName = function() {
	var mthName = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
	return mthName[this.getMonth()];
}
Date.prototype.getDayName = function() {
	var dayName = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
	return dayName[this.getDay()];
function GetToday(full)	// returns a string with today's date
{						// if full is 'true', returns a fuller version of the date (incl. Thu, Jan)
	( this.currDT ) ? this.currDT.setSeconds(this.currDT.getSeconds()+3) : this.currDT = new Date();
	this.currDate = currDT.getDate();
	this.currMonth = currDT.getMonth()+1;
	this.currYr = currDT.getFullYear();
	this.currHr = currDT.getHours();
	this.currMin = currDT.getMinutes();
	this.currTime = ( currHr < 10 ? "0" : "" ) + currHr + ':' + ( currMin < 10 ? "0" : "" ) + currMin;
	currDate = ( currDate < 10 ? "0" : "" ) + currDate;			// Pad with leading zeros, if required
	currMonth = ( currMonth < 10 ? "0" : "" ) + currMonth;
	if ( full )
		return currDT.getDayName() + ' ' + currDate + ' ' + currDT.getMonthName() + ' ' + currYr + ' ' + currTime;
	else
		return currDate + '/' + currMonth + '/' + currYr + ' ' + currTime;
}

var UpdateClock = function () { $('myClock').firstChild.nodeValue = GetToday(true); }
window.setInterval(UpdateClock, 3000);
(I don't need it to update every second or to be 100% accurate.) Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
Leif
Administrator
Posts: 7192
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Clocking off

Post by Leif »

At a guess, it (the date) is going to be wrong if viewed any day over midnight
Leif

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Clocking off

Post by agibsonsw »

Hello.
I wondered about that, but am not sure why? It's managing to tick over the hour mark so why would it fail at midnight please?

(Looks like I've a long night ahead of me..) Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
Leif
Administrator
Posts: 7192
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Clocking off

Post by Leif »

Maybe I misunderstood - I read it that you were only updating the time element every 3 seconds, rather than the whole date and time...

Could you test it by setting your PC time to a minute before midnight?
Leif

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Clocking off

Post by agibsonsw »

I've set it to 10 to midnight (so that I don't have to correct the minutes and seconds!). I'm getting goosebumps waiting :flee:

I was reluctant to test it this way as there's nothing like 'real-time'. But I suppose I can't wait 50 years to be convinced. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Clocking off

Post by agibsonsw »

Hi.
Seems okay. Using Date() obtains the full date/time. Adding three seconds to it works okay.

Do you have the correct time? When's the next leap year? Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
Leif
Administrator
Posts: 7192
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Clocking off

Post by Leif »

Of course I have the right time - it's Friday, right? :grin:

Next leap year should be 2012, but I reckon if it works over midnight, it should be good whenever.
Leif

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Clocking off

Post by agibsonsw »

Ta.

Is it sensible to use 'this' often within a function? It changes a variable to a (static) property of the function 'stroke' object. I'm using it whenever it doesn't need to be re-invented on each function call.

Also, would it be possible for me to put the prototype day and month names within the UpdateClock function (as it's only required for this)? Will this remove its global scope?

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.