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 1. Cartesian product phenomenon...
1. Docker imports local images Sometimes we copy ...
Table of contents 1. MySQL Architecture 2. Networ...
The css technique for changing the color of an im...
HTML 4 HTML (not XHTML), MIME type is text/html, ...
Table of contents Step 1: Build the framework Ste...
【background】 I encountered a very embarrassing th...
When the author was using MySQL to add a user, he...
Preface When developing static pages, such as Vue...
1. Introduction Today a colleague asked me how to...
Table of contents Preface 1. Set the prototype on...
Copy code The code is as follows: <!--[if !IE]...
This article example shares the specific code of ...
Table of contents 1. Nginx implements load balanc...
Table of contents Optimization of if judgment 1. ...