SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 86

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

 

Tips…

     We’re in the home stretch. We’ve grabbed the time, set the returns into numbers we can play with and used those numbers to post the AM/PM image and the two hour images. Now we’ll set the minutes and seconds.







Here’s the Code


     Here’s the code we’re interested in:

if(min < 10) {

document[”tensMin”].src=d[0].src;

}

if(min > 9) {

document[”tensMin”].src=d[parseInt(min/10,10)].src;

}

document[”Min”].src=d[min%10].src;

if(sec < 10) {

document[”tensSec”].src=d[0].src;

}

if(sec > 9) {

document[”tensSec”].src=d[parseInt(sec/10,10)].src;

}

document[”Sec”].src=d[sec%10].src;

document[”amPM”].src=amPM;

setTimeout(“clock();”,100);

     The script plays with the minutes and seconds in the same way it did the hours, through “if” statements.

     If the minute return is less than “tensMIN” (the first minute image), it is set to d0.src. If you look back up the script, you’ll see that is the dgt0.gif.

     If the minute is more than nine, then we need to get the correct 10 number in the “tensMin” position. We already know that the minute return is 10 or above so we need either 1, 2, 3, 4, or 5. The little math equation parseInt(min/10,10) does the trick.



TechCrawler


Want more information about
Javascript clocks?
Search the Web.



     ParseInt is a JavaScript function that turns a text string into an integer and returns that integer. Let’s say that the minute return is 43. If you divide 43 by 10, you get 4.3. The second number allows you to set the base of the integer returned. In this case, ten is the base. That gets rid of the “point whatever” extension. The number returned is 4. Success.

     Now that we have the first minute image, we need the second. That is done, again, by a quick mathematic equation. Tha value assigned to “Min” is arrived at by taking the minute return and dividing it by ten and returning the remainder. That’s what the percentage sign does.

     For example, the minute return is is 36. If you divide that by ten, you get 3.6. The remainder is six, six is returned. Success…again.

     The seconds are done exactly the same way following the same math using the seconds return.

     Finally the “amPM” flag is given the value represented by the variable “amPM” from above. Remember that? We set it almost first in the script.

     OK, we’ve posted all of the images. The problem is that the second changes, well, every second. We need get this script to run again and again to repost again and again. The effect is a running digital clock.

     The “setTimeout()” method does that. It’s set to run the function clock() every 100/1000ths of a second. That should do it.

     Next time around we’ll stay with a digital clock, except we’ll create a clock that will tell you the time in any one of the time zones anywhere in the world. It’s a big, involved script that allows for daylight savings time and leap years. You’ll like it.

     See you next time.

Next Week: Time Zone Clock



     Do YOU have a Script Tip you’d like to share? How about suggesting a Script Tip to write about? I’d love to hear it. Write me at:
jburns@htmlgoodies.com.

Learn to write your own JavaScripts with the

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.