JavaScript to achieve digital clock effect

JavaScript to achieve digital clock effect

This article example shares the specific code of JavaScript to achieve the digital clock effect for your reference. The specific content is as follows

Rendering

Demand Analysis

1. Get time through date
2. Dynamically obtain time through interval timer setInterval
3. Set the interval of the interval timer setInterval to 1000 milliseconds (1 second) to get the time once
4. For a good-looking style, we use digital pictures instead of numbers.

source code

HTML Part

<div id="div">
 <img src="img/0.png" />
 <img src="img/0.png" />
 <img src="img/0.png" />
 <img src="img/0.png" />
 <img src="img/0.png" />
 <img src="img/0.png" />
 seconds</div>

CSS part

<style>
/*none*/ 
</style>

JavaScript

<script>
 // Requirement: digital clock var date = new Date();
 var imgArr = document.getElementsByTagName('img'); //Get a collection of img pictures var hours, minutes, seconds;
 var time = setInterval(function () {
 date = new Date();
 // Get hours hours = date.getHours();
 
 imgArr[0].src = "img/" + parseInt(hours / 10) + ".png"
 imgArr[1].src = "img/" + hours % 10 + ".png"
 // Get minutes minutes = date.getMinutes();
 imgArr[2].src = "img/" + parseInt(minutes / 10) + ".png"
 imgArr[3].src = "img/" + minutes % 10 + ".png"
 // Get seconds seconds = date.getSeconds();
 imgArr[4].src = "img/" + parseInt(seconds / 10) + ".png"
 imgArr[5].src = "img/" + seconds % 10 + ".png"
 console.log(hours);
 console.log(minutes);
 console.log(seconds);
 }, 1000)
</script>

Total code

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
</head>
<body>
 <div id="div">
 <img src="img/0.png" />
 <img src="img/0.png" />
 <img src="img/0.png" />
 <img src="img/0.png" />
 <img src="img/0.png" />
 <img src="img/0.png" />
 seconds</div>
</body>

</html>
<script>
 // Requirement: digital clock var date = new Date();
 var imgArr = document.getElementsByTagName('img'); //Get a collection of img pictures var hours, minutes, seconds;
 var time = setInterval(function () {
 date = new Date();
 // Get hours hours = date.getHours();
 
 imgArr[0].src = "img/" + parseInt(hours / 10) + ".png"
 imgArr[1].src = "img/" + hours % 10 + ".png"
 // Get minutes minutes = date.getMinutes();
 imgArr[2].src = "img/" + parseInt(minutes / 10) + ".png"
 imgArr[3].src = "img/" + minutes % 10 + ".png"
 // Get seconds seconds = date.getSeconds();
 imgArr[4].src = "img/" + parseInt(seconds / 10) + ".png"
 imgArr[5].src = "img/" + seconds % 10 + ".png"
 }, 1000)
</script>

Images used:

Since you don't have pictures, directly copying the code will not show the effect. You can find a few pictures to replace them and modify them slightly. As long as you can understand the code, there will be no problem in modifying it.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue.js implements a digital clock function example with date and week
  • JS+CSS to achieve scrolling digital clock effect
  • js realizes a simple digital clock effect
  • Use JS to display the countdown digital clock effect
  • JavaScript digital clock example to achieve scrolling effect
  • JS realizes the countdown digital clock effect [with example code]
  • Web page countdown digital clock effect implemented by JS
  • JavaScript digital clock example sharing
  • HTML5 canvas js (digital clock) example code
  • JavaScript to implement dynamic digital clock

<<:  Detailed explanation of CentOS configuration of Nginx official Yum source

>>:  4 Ways to Quickly Teach Yourself Linux Commands

Recommend

Share some key interview questions about MySQL index

Preface An index is a data structure that sorts o...

Detailed explanation of mysql replication tool based on python

Table of contents 1. Introduction Second practice...

Nginx one domain name to access multiple projects method example

Background Recently, I encountered such a problem...

Example of viewing and modifying MySQL transaction isolation level

Check the transaction isolation level In MySQL, y...

How to modify the IP restriction conditions of MySQL account

Preface Recently, I encountered a requirement at ...

How to limit the value range of object keys in TypeScript

When we use TypeScript, we want to use the type s...

Detailed process of using Vscode combined with docker for development

Preface Using Docker and VS Code can optimize the...

How to use HTML+CSS to create TG-vision homepage

This time we use HTML+CSS layout to make a prelim...

Detailed explanation of how to create MySql scheduled tasks in navicat

Detailed explanation of creating MySql scheduled ...

How to solve the mysql error 1033 Incorrect information in file: 'xxx.frm'

Problem Description 1. Database of the collection...

How to optimize MySQL indexes

1. How MySQL uses indexes Indexes are used to qui...

Linux completely removes node.js and reinstalls it through the yum command

first step Delete it once with the built-in packa...

Pure CSS to implement iOS style open and close selection box function

1 Effect Demo address: https://www.albertyy.com/2...

Detailed explanation of Vue development website SEO optimization method

Because the data binding mechanism of Vue and oth...