Javascript: an elegant clock

Source: Internet
Author: User
Tags setinterval

Implementation results:

Preparatory work:

Knowledge about the # Timer

* JavaScript Date (date) object

3# the picture used to prepare the effect

Implementation principle:

# gets the current time;

var time=new Date (), Var ihours=time.gethours (), Var iminutes=time.getminutes (), Var iseconds=time.getseconds (), Var Inow=double (ihours) + ': ' +double (iminutes) + ': ' +double (iseconds);

set timer, 1s to execute once;

SetInterval (function () {code//Code section},1000);

3# dynamically changes the corresponding IMG attribute value via real time data

3.1 Method One: The fixed position picture displays the corresponding time data

Arrimg[0].src= ' img/' +parseint (IHOURS/10) + '. jpg '; arrimg[1].src= ' img/' +ihours%10+ '. jpg ';

  

3.2 method Two: Get the date string by Charat () specify the position character and then change the IMG property value of the corresponding picture

Arrimg[i].src= ' img/' +inow.charat (i) + '. jpg ';

  

Code section:

method one:  fixed position picture displays corresponding time data

<! DOCTYPE html>

Method Two: Get the date string by Charat () to specify the position character and then change the IMG property value of the corresponding picture

<! DOCTYPE html>

  

Javascript: an elegant clock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.