This article mainly introduces how to use JS to implement a simple digital clock, which has certain reference value. Now I share it with you. Friends in need can refer to it
Design ideas:
The digital clock displays the current time through picture numbers, and the URL of the picture to be displayed changes according to the time.
a. Get the current time Date() and convert the current time information into a 6-digit string;
b. Change the image according to the number corresponding to each position of the time string The value of src, so as to replace the displayed image;
Build the HTML foundation and add styles.
<div id="div1"> <img src="/static/imghwm/default1.png" data-src="./数字时钟(1)_files/" class="lazy" src='./数字时钟(1)_files/0.jpg' alt="How to use JS to implement a simple digital clock" > <img src="/static/imghwm/default1.png" data-src="./数字时钟(1)_files/" class="lazy" src='./数字时钟(1)_files/0.jpg' alt="How to use JS to implement a simple digital clock" > : <img src="/static/imghwm/default1.png" data-src="./数字时钟(1)_files/" class="lazy" src='./数字时钟(1)_files/0.jpg' alt="How to use JS to implement a simple digital clock" > <img src="/static/imghwm/default1.png" data-src="./数字时钟(1)_files/" class="lazy" src='./数字时钟(1)_files/0.jpg' alt="How to use JS to implement a simple digital clock" > : <img src="/static/imghwm/default1.png" data-src="./数字时钟(1)_files/" class="lazy" src='./数字时钟(1)_files/0.jpg' alt="How to use JS to implement a simple digital clock" > <img src="/static/imghwm/default1.png" data-src="./数字时钟(1)_files/" class="lazy" src='./数字时钟(1)_files/0.jpg' alt="How to use JS to implement a simple digital clock" > </div>
style style
#p1{ width:50%; margin:300px auto; background:black; }
Get the picture element and current time:
var op=document.getElementById('p1'); var aImg=op.getElementsByTagName('img'); var oDate=new Date(); var str=oDate.getHours()+oDate.getMinutes()+oDate.getSeconds();
There are two problems here
1. Returned by oDate.getHours() They are all numbers, so it is written as the addition of numbers, not the addition of strings.
2. When the obtained value is a single digit, the number of strings will be less than 6, which does not meet the initial design requirements.
Code solution to solve the above two problems:
var op=document.getElementById('p1'); var aImg=op.getElementsByTagName('img'); var oDate=new Date(); function twoDigitsStr() { if(n<10) {return '0'+n;} else {return ''+n;} } var str=twoDigitsStr(oDate.getHours())+twoDigitsStr(oDate.getMinutes())+twoDigitsStr(oDate.getSeconds());
Set the src value of all pictures:
for(var i=0;i<aimg.length><p> Use setInterval() to achieve re-acquisition every 1 second Current time and image src value: </p><pre class='brush:php;toolbar:false;'> var op=document.getElementById('p1'); var aImg=op.getElementsByTagName('img'); setInterval(function tick() { var oDate=new Date(); var str=twoDigitsStr(oDate.getHours())+twoDigitsStr(oDate.getMinutes())+twoDigitsStr(oDate.getSeconds()); for(var i=0;i<aImg.length;i++) { aImg[i].src="./数字时钟(1)_files/"+str.charAt(i)+".jpg"; } } ,1000);
But there is still a problem. In the initial stage of opening the web page, the display time is 00:00:00. This is because the timer has a characteristic. When the timer is turned on After that, the function inside will not be executed immediately, but will be executed after 1 second. Solution code:
var op=document.getElementById('p1'); var aImg=op.getElementsByTagName('img'); function tick() { var oDate=new Date(); var str=twoDigitsStr(oDate.getHours())+twoDigitsStr(oDate.getMinutes())+twoDigitsStr(oDate.getSeconds()); for(var i=0;i<aImg.length;i++) { aImg[i].src="./数字时钟(1)_files/"+str.charAt(i)+".jpg"; } } setInterval(tick,1000); tick();
Question: The function tick in the code setInterval(tick,1000); does not have parentheses, so what is the difference between functions in JS with parentheses and without parentheses?
The complete digital clock production JS code is:
function twoDigitsStr(n) { if(n<10) {return '0'+n;} else {return ''+n;} } window.onload=function() { var op=document.getElementById('p1'); var aImg=op.getElementsByTagName('img'); function tick() {var oDate=new Date(); var str=twoDigitsStr(oDate.getHours())+twoDigitsStr(oDate.getMinutes())+twoDigitsStr(oDate.getSeconds()); for(var i=0;i<aImg.length;i++) { aImg[i].src="./数字时钟(1)_files/"+str.charAt(i)+".jpg"; } } setInterval(tick,1000); tick(); }
Of course, if you have good creative pictures, you can replace the above digital pictures to generate various dazzling digital clock effects. For example:
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Share common extensions for js objects
Share the complete way of Nodejs accessing WeChat JS-SDK process
The above is the detailed content of How to use JS to implement a simple digital clock. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
