Jump to content

JavaScript:Tutorial, Digital Clock

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Introduction:-
This is my second tutorial and I will show you how to get the Time in a Text Box.. perhaps in a Text Box it will be useless, but it could be used in other instances!

Solution:-
This code goes in the <head> tags:-

<script language="JavaScript">

var timerID = null;
var timerRunning = false;

function stopclock (){
        if(timerRunning)
                clearTimeout(timerID);
        timerRunning = false;
}

function showtime () {
        var now = new Date();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds()
        var timeValue = hours
        timeValue += ((minutes < 10) ? ":0" : ":") + minutes
        timeValue += ((seconds < 10) ? ":0" : ":") + seconds
        document.clock.face.value = timeValue;
        timerID = setTimeout("showtime()",1000);
        timerRunning = true;
}
function startclock () {
        stopclock();
        showtime();
}

</script>

And this code as the body code:-

<body onLoad="startclock()">

<form name="clock" onSubmit="0">
  <div align="center"><center><p><input type="text" name="face" size="10" value> </p>
  </center></div>
</form>

Explanation:-
I think its simple without any need of explanation.. but if you have any suggestions/questions just post them here :)

A Preview:-
Posted Image

Conclusion:-
As Always Feedback is welcome and the full source is attached!!

Attached Files



#2
xtraze

xtraze

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 910 posts
Nice, and I am now looking for an Analog clock now. Hope I can find it by Google. I will search tomorrow.

#3
AfTriX

AfTriX

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 586 posts
Thanks for the code. very useful. Text Box clock idea is good, and pretty simple code.

#4
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts

xtraze said:

Nice, and I am now looking for an Analog clock now. Hope I can find it by Google. I will search tomorrow.

http://forum.codecal...alog-clock.html

I found this one for you!

#5
AfTriX

AfTriX

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 586 posts
Seems Tcm9669 continuously digging on JavaScript mats. Nice work and thanks

#6
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts

AfTriX said:

Seems Tcm9669 continuously digging on JavaScript mats. Nice work and thanks

Welcome :)
I just like JavaScript

#7
AfTriX

AfTriX

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 586 posts

Tcm9669 said:

Welcome :)
I just like JavaScript

Nice to here. Keep on improving your skills..

#8
xtraze

xtraze

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 910 posts
Thanks for it lol. Nice I will add it as an html mod into a Joomla powered website I'm currently working on.

#9
AfTriX

AfTriX

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 586 posts

xtraze said:

Thanks for it lol. Nice I will add it as an html mod into a Joomla powered website I'm currently working on.

Don NOT forget to inform all the CodeCall members to tell about your site once its completed

#10
doosa

doosa

    Newbie

  • Members
  • Pip
  • 1 posts
good script

#11
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Welcome. JS is quite simple to master.

#12
cryptokyle

cryptokyle

    Newbie

  • Members
  • PipPip
  • 11 posts
<input type="text" name="face" size="10" value>

why does the value not have ="" ? does it work the same as value="0"?