This article example shares the specific code of javascript to achieve the countdown effect for your reference. The specific content is as follows Implementation ideas: 1. Create label elements for days, hours, minutes, and seconds on the page and define the styles Code example:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Countdown effect</title> <style> div { margin: 200px; } span { display: block; float: left; width: 50px; height: 50px; line-height: 50px; text-align: center; margin: 5px; color: #fff; background-color: rgba(0, 0, 0, .8); } </style> </head> <body> <div class="time"> <span class="day">0</span> <span>day</span> <span class="hour">1</span> <span>hours</span> <span class="minute">2</span> <span> points</span> <span class="second">3</span> <span>seconds</span> </div> <script> var day = document.querySelector('.day'); var hour = document.querySelector('.hour'); var minute = document.querySelector('.minute'); var second = document.querySelector('.second'); var setTime = +new Date('2021-10-1 09:00:00'); countDown(); setInterval(countDown, 1000); console.log(+new Date()); function countDown() { var now = +new Date(); var times = (setTime - now) / 1000; var d = parseInt(times / 60 / 60 / 24); //day d = d < 10 ? '0' + d : d; day.innerHTML = d; //Give the remaining days to the day box var h = parseInt(times / 60 / 60 % 24); //When h = h < 10 ? '0' + h : h; hour.innerHTML = h; //Give the remaining hours to the hour box var m = parseInt(times / 60 % 60); //minutes m = m < 10 ? '0' + m : m; minute.innerHTML = m; //divide the rest into the points box var s = parseInt(times % 60); //seconds s = s < 10 ? '0' + s : s; second.innerHTML = s; //Give the remaining seconds to the seconds box} </script> </body> </html> Note: When calling the setInterval function, just write the function name. Do not write parameters in the function. If you write parameters, the function may not be executed in a loop. Page effect: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:
|
<<: How to maintain a long connection when using nginx reverse proxy
>>: The advantages and disadvantages of nginx and lvs and their suitable usage environment
Table of contents Preface 1. What is phantom read...
Click here to return to the 123WORDPRESS.COM HTML ...
<p><b>This is bold font</b></...
This article introduces in detail some of the tech...
Keepalived+Nginx+Tomcat to achieve high availabil...
I won't say much nonsense, let's just loo...
Recently, Xiao Ming just bought a new Mac and wan...
Differences between Docker and Docker Machine Doc...
Preface: This article only introduces the steps t...
500 (Internal Server Error) The server encountere...
When it comes to bionic design, many people will t...
1. Simple configuration of nginx's dynamic an...
The first step is to add the corresponding databa...
Table of contents Preface Summary of the principl...
This article example shares the specific code of ...