Use native JavaScript to simply implement the countdown, for your reference, the specific content is as follows Effect Code // An highlighted block <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <!-- css style --> <style type="text/css"> * { margin: 0; padding: 0; } .onsell { height: 400px; width: 200px; border: 1px solid #F2F2F2; margin: 10px; box-shadow: 1px 2px 4px rgba(0, 0, 0, .2); } .box { /* display: none; */ float: left; margin: 328px 34px 0; } .box div { position: relative; display: inline-block; width: 40px; height: 40px; background-color: #333; color: #fff; font-size: 20px; text-align: center; line-height: 40px; box-shadow: 1px 2px 4px rgba(0, 0, 0, .4); } </style> </head> <body> <!-- Requirement: A product will be on sale for one day in the future, and a countdown effect will be created using js: hours: minutes: seconds --> <div class="onsell"> <div class="box"> <div class="hour">00</div> <div class="minutes">00</div> <div class="seconds">00</div> </div> </div> </body> </html> <script> window.onload = function () { let hour = document.querySelector('.hour') let minutes = document.querySelector('.minutes') let seconds = document.querySelector('.seconds') // The countdown timestamp uses + to convert the time object into a timestamp, which is equivalent to Date.now() let wantTime = +new Date('2021-3-17 18:00:00') countTime() let timer = setInterval(() => { countTime() }, 1000) function countTime() { let currentTime = +new Date() if (wantTime >= currentTime) { let times = (wantTime - currentTime) / 1000 // Total seconds timestamp / 1000 = seconds let remainDay = parseInt(times / 60 / 60 / 24) // The remainder is the remaining days console.log(remainDay); if (remainDay === 0) { if(times < 1) { // Countdown is over // Trigger operation here} // If the number of days is less than one day, start timing setTime(times) } } else { hour.innerHTML = '00' minutes.innerHTML = '00' seconds.innerHTML = '00' } } function setTime(time) { // Rough version let s = parseInt(time % 60) s = s < 10 ? '0' + s : s let m = parseInt(time / 60 % 60) m = m < 10 ? '0' + m : m let h = parseInt(time / 60 / 60 % 24) h = h < 10 ? '0' + h : h hour.innerHTML = h minutes.innerHTML = m seconds.innerHTML = seconds } } </script> 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:
|
<<: Detailed explanation of software configuration using docker-compose in linux
>>: Detailed explanation of replace into example in mysql
Table of contents 1. Demand 1. Demand 2. SDK para...
Install Install ts command globally npm install -...
How to turn a jar package into a docker container...
Table of contents Preface 1. Why do we need bread...
This article introduces the development environme...
Of course, there are many people who hold the oppo...
Layout part: <div id="slider"> &l...
Table of contents 1. Numeric Type 1.1 Classificat...
The MySQL database does not have an incremental b...
1. Generally, mariadb is installed by default in ...
Solution to the problem that there is no unzip co...
Knowing which for loop or iterator is right for o...
The implementation method is divided into three s...
1. Configure local yum source 1. Mount the ISO im...
Lock classification: From the granularity of data...