Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.java.* > Newsgroup comp.lang.javascript

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 12-30-2006, 09:59 PM
cenktarhancenk@gmail.com
Guest
 
Posts: n/a
Default clock in javascript

is there a way to display a ticking clock in a web page using
javascript? but not in a textbox, rather as text that i can change the
style, font etc.

cenk tarhan

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 12-30-2006, 10:05 PM
VK
Guest
 
Posts: n/a
Default Re: clock in javascript


cenktarhancenk@gmail.com wrote:
> is there a way to display a ticking clock in a web page using
> javascript? but not in a textbox, rather as text that i can change the
> style, font etc.


<http://www.dynamicdrive.com/dynamicindex6/clock2.htm>

see <http://www.dynamicdrive.com/dynamicindex6/> for more

Reply With Quote
  #3 (permalink)  
Old 12-30-2006, 10:45 PM
cenktarhancenk@gmail.com
Guest
 
Posts: n/a
Default Re: clock in javascript

thanks a lot VK, for the qucikest reply ever!

Reply With Quote
  #4 (permalink)  
Old 12-30-2006, 10:50 PM
pcx99
Guest
 
Posts: n/a
Default Re: clock in javascript

cenktarhancenk@gmail.com wrote:
> is there a way to display a ticking clock in a web page using
> javascript? but not in a textbox, rather as text that i can change the
> style, font etc.
>
> cenk tarhan
>

<html>
<head>
<title>Clock</title>
</head>
<body>
<body>
<div id='clock' class='somecssstyle'></div>

<script langage='javascript :-P' type='text/javascript'>
var months=new Array("January", "February", "Mararch", "April",
"May", "June", "Julu", "August", "September", "October", "November",
"December");
var calDays=new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
var clockID = document.getElementById('clock');

function pad(i) {
//simple function to just add a zero in front of numbers less
than zero (so we get 12:05 instead of 12:5)
if (i < 10) {
i = "0"+i;
}
return(i);
}

function doClock() {
setTimeout( "doClock()", 1000 );
t = new Date();
m = t.getMonth();
d = t.getDay();
dt = t.getDate();
y = t.getFullYear();
h = t.getHours();
if (h < 12) {
ap="AM";
} else {
ap="PM";
h=h-12;
}
mn= pad(t.getMinutes());
s = pad(t.getSeconds());
if (h==0) {
h = 12
}
clockID.innerHTML=calDays[d]+", "+months[m]+" "+dt+"
"+y+"<BR>"+h+":"+mn+":"+s+" "+ap;
}

doClock()
</script>
</body>

</html>

Happy new year -- or else!



--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Reply With Quote
  #5 (permalink)  
Old 12-31-2006, 06:17 AM
RobG
Guest
 
Posts: n/a
Default Re: clock in javascript

pcx99 wrote:
> cenktarhancenk@gmail.com wrote:
>> is there a way to display a ticking clock in a web page using
>> javascript? but not in a textbox, rather as text that i can change the
>> style, font etc.

[...]
> <div id='clock' class='somecssstyle'></div>
>
> <script langage='javascript :-P' type='text/javascript'>


Was there ever a script element attribute called 'langage'? ;-)


> var months=new Array("January", "February", "Mararch", "April",
> "May", "June", "Julu", "August", "September", "October", "November",
> "December");


I think you need to use a spell checker.


> var calDays=new Array("Sunday", "Monday", "Tuesday", "Wednesday",
> "Thursday", "Friday", "Saturday");
> var clockID = document.getElementById('clock');
>
> function pad(i) {
> //simple function to just add a zero in front of numbers less than
> zero (so we get 12:05 instead of 12:5)
> if (i < 10) {
> i = "0"+i;
> }
> return(i);
> }


A more concise function for this case is:

function pad(i){
return (i<10)? '0'+i : ''+i;
}


> function doClock() {
> setTimeout( "doClock()", 1000 );


Simply setting a timeout of 1000 ms does not guarantee that the function
will run at exactly 1 second intervals, otherwise you could just use
setInterval.


> t = new Date();
> m = t.getMonth();
> d = t.getDay();
> dt = t.getDate();
> y = t.getFullYear();
> h = t.getHours();
> if (h < 12) {
> ap="AM";
> } else {
> ap="PM";
> h=h-12;
> }
> mn= pad(t.getMinutes());
> s = pad(t.getSeconds());
> if (h==0) {
> h = 12


I never understood the concept of representing times between midnight
and 1:00 am as 12:xx am. Why not 00:xx am? There is far less chance of
confusion. A large percentage of the world uses a 24hr clock, so maybe
that's a better idea.

I don't know why anyone wants to put an clock in a web page unless it's
to display the time somewhere other that where the visitor is located.
Anyhow, here's another effort. I find changing the value of a text node
causes less flicker and is faster in some browsers than changing the
innerHTML.

<script type="text/javascript">

function Clock(id) {
if (typeof id == 'string') id = document.getElementById(id);
this.el = id;
}

Clock.prototype.month = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];

Clock.prototype.day = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];

Clock.prototype.addZ = function(n){ return (n<10)? '0'+n: ''+n; };

Clock.prototype.tick = function(){
var clock = this;
var t = new Date();
this.el.firstChild.data = clock.day[t.getDay()] + ', '
+ clock.addZ(t.getDate()) + ' '
+ clock.month[t.getMonth()] + ' '
+ t.getFullYear() + ', '
+ clock.addZ(t.getHours()) + ':'
+ clock.addZ(t.getMinutes()) + ':'
+ clock.addZ(t.getSeconds());

setTimeout(function(){clock.tick();}, (1.05 - t.getMilliseconds()));
}

</script>

<span id="clock">&nbsp;</span>
<script type="text/javascript">
var digClock = new Clock('clock'); digClock.tick();
</script>


--
Rob
Reply With Quote
  #6 (permalink)  
Old 12-31-2006, 11:02 AM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: clock in javascript

In comp.lang.javascript message <1167519559.295969.19160@a3g2000cwd.goog
legroups.com>, Sat, 30 Dec 2006 14:59:19, cenktarhancenk@gmail.com
posted:
>is there a way to display a ticking clock in a web page using
>javascript? but not in a textbox, rather as text that i can change the
>style, font etc.


<URL:http://www.merlyn.demon.co.uk/js-date2.htm>
<URL:http://www.merlyn.demon.co.uk/js-anclk.htm>

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Reply With Quote
  #7 (permalink)  
Old 12-31-2006, 09:26 PM
Lee
Guest
 
Posts: n/a
Default Re: clock in javascript

RobG wrote:

> I never understood the concept of representing times between midnight
> and 1:00 am as 12:xx am.


That's how it's been represented on most analog clocks for centuries.


Reply With Quote
  #8 (permalink)  
Old 12-31-2006, 10:02 PM
Fred
Guest
 
Posts: n/a
Default Re: clock in javascript

Lee wrote:
> RobG wrote:
>
> > I never understood the concept of representing times between midnight
> > and 1:00 am as 12:xx am.

>
> That's how it's been represented on most analog clocks for centuries.


Which doesn't explain why it should be used with digital clocks based
on 24 hr time, even those that wish to use 12 hour intervals with am
and pm. 00:15 (am) is unambiguous, 12:15 am can easily be
misunderstood. 12:00 am and 12:00 pm make no sense and create
confusion.

Only a small proportion of the world's population uses 12 hour notation
with am/pm, 24 hr notation is preferable on the web.

Happy new year :-)


--
Rob

Reply With Quote
  #9 (permalink)  
Old 01-01-2007, 04:33 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: clock in javascript

In comp.lang.javascript message <459763f0$0$2605$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Sun, 31 Dec 2006 17:17:01, RobG
<rgqld@iinet.net.au> posted:
>pcx99 wrote:


>A more concise function for this case is:
>
>function pad(i){
> return (i<10)? '0'+i : ''+i;
>}


or

function pad(i) { return (i<10 ? '0' : '') + i }



> setTimeout(function(){clock.tick();}, (1.05 - t.getMilliseconds()));
>}


I think you mean 1050 not 1.05 ; I wonder how that method compares in
efficiency with (1050 - t%1000), and how one could tell.

For anywhere using ISO 8601 time format, something like
new Date().toString().match(/[\d:]{8}/)
or new Date().toString().match(/([\d:]{8})/)[1]
or new Date().toString().match(/\s(\d+:[\d:]+)\s/)[1]
will give hh:mm:ss.

Since the OP wanted a clock, I see no need to provide a calendar
function.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Reply With Quote
  #10 (permalink)  
Old 01-01-2007, 10:29 PM
Lee
Guest
 
Posts: n/a
Default Re: clock in javascript

Fred wrote:
> Lee wrote:
>> RobG wrote:
>>
>>> I never understood the concept of representing times between midnight
>>> and 1:00 am as 12:xx am.

>> That's how it's been represented on most analog clocks for centuries.

>
> Which doesn't explain why it should be used with digital clocks based
> on 24 hr time, even those that wish to use 12 hour intervals with am
> and pm. 00:15 (am) is unambiguous, 12:15 am can easily be
> misunderstood.


"12:15 am" cannot easily be misunderstood by anyone who understands
what am and pm mean. "12:00 am" seems to be a problem for people who
have trouble with concept formation.

I personally perfer 24-hour time notation, but if you're going to
use am and pm, the midnight hour is correctly represented as "12".
Reply With Quote
  #11 (permalink)  
Old 01-02-2007, 04:09 AM
RobG
Guest
 
Posts: n/a
Default Re: clock in javascript

Dr J R Stockton wrote:
> In comp.lang.javascript message <459763f0$0$2605$5a62ac22@per-
> qv1-newsreader-01.iinet.net.au>, Sun, 31 Dec 2006 17:17:01, RobG
> <rgqld@iinet.net.au> posted:

[...]
> > setTimeout(function(){clock.tick();}, (1.05 - t.getMilliseconds()));
> >}

>
> I think you mean 1050 not 1.05 ;


Yes - 1.05 makes the clock update rather too often :-x


> I wonder how that method compares in
> efficiency with (1050 - t%1000), and how one could tell.


For a single call? Impossible using javascript I think, it probably
requires acccurate measurement of nanoseconds. :-) Running a loop of
10,000 calls shows getMilliseconds is 20% faster in Safari but takes 3
times longer than the remainder operator in Firefox on OS X.


> For anywhere using ISO 8601 time format, something like
> new Date().toString().match(/[\d:]{8}/)
> or new Date().toString().match(/([\d:]{8})/)[1]
> or new Date().toString().match(/\s(\d+:[\d:]+)\s/)[1]
> will give hh:mm:ss.
>
> Since the OP wanted a clock, I see no need to provide a calendar
> function.


I was thinking the same thing - if a 24hr clock suits then:

<script type="text/javascript">
function runClock(el) {
if (typeof el == 'string') el = document.getElementById(el);
var t = new Date();
el.firstChild.data = t.toString().match(/([\d:]{8})/)[1];
setTimeout( function(){runClock(el);}, 1050 - t%1000 );
}
</script>

<div id="digiClock">&nbsp;</div>
<script type="text/javascript">runClock('digiClock');</script>


--
Rob

Reply With Quote
  #12 (permalink)  
Old 01-02-2007, 05:33 PM
John W. Kennedy
Guest
 
Posts: n/a
Default Re: clock in javascript

Lee wrote:
> Fred wrote:
>> Lee wrote:
>>> RobG wrote:
>>>
>>>> I never understood the concept of representing times between midnight
>>>> and 1:00 am as 12:xx am.
>>> That's how it's been represented on most analog clocks for centuries.

>>
>> Which doesn't explain why it should be used with digital clocks based
>> on 24 hr time, even those that wish to use 12 hour intervals with am
>> and pm. 00:15 (am) is unambiguous, 12:15 am can easily be
>> misunderstood.

>
> "12:15 am" cannot easily be misunderstood by anyone who understands
> what am and pm mean. "12:00 am" seems to be a problem for people who
> have trouble with concept formation.


To be absolutely strict, it is "12:00 noon" and "12:00 midnight".
Neither one of the two is either AM or PM. However, a great many
mechanical and electronic time displays have problems with this, and so
"12:00 a.m." has come to be used to mean "12:00 midnight" and "12:00
p.m." to mean "12:00 noon".

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
Reply With Quote
  #13 (permalink)  
Old 01-02-2007, 06:09 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: clock in javascript

John W. Kennedy wrote on 02 jan 2007 in comp.lang.javascript:

> To be absolutely strict, it is "12:00 noon" and "12:00 midnight".
> Neither one of the two is either AM or PM. However, a great many
> mechanical and electronic time displays have problems with this, and so
> "12:00 a.m." has come to be used to mean "12:00 midnight" and "12:00
> p.m." to mean "12:00 noon".
>


.... which does not stop us in the rest of the world being amazed over.

It is completely illogical, but nicely explained, john.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can php replace javascript approach? Geoff Cox Newsgroup comp.lang.php 39 04-11-2009 12:00 AM
javascript strings not 7 bit like I expected Stevo Newsgroup comp.lang.javascript 9 04-02-2009 10:05 AM
2x Clock Multiplier Help GamEmpire Newsgroup comp.lang.verilog 2 04-02-2009 02:45 AM
SetTimeout question... Thomas Allen Newsgroup comp.lang.javascript 32 04-01-2009 12:30 PM
FAQ Topic - Internationalisation and Multinationalisation in javascript. (2009-03-31) FAQ server Newsgroup comp.lang.javascript 0 03-30-2009 11:00 PM



All times are GMT. The time now is 12:08 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.