JavaScript implements countdown on front-end web page

JavaScript implements countdown on front-end web page

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 for loop and double for loop in JavaScript
  • JavaScript to implement image preloading and lazy loading
  • Five ways to traverse JavaScript arrays
  • In-depth understanding of javascript class array
  • JavaScript implements the protocol example in which the user must check the box

<<:  Detailed explanation of software configuration using docker-compose in linux

>>:  Detailed explanation of replace into example in mysql

Recommend

Deployment and Chinese translation of the docker visualization tool Portainer

#docker search #docker pull portainer 1. Download...

element-ui Mark the coordinate points after uploading the picture

What is element-ui element-ui is a desktop compon...

IDEA configuration process of Docker

IDEA is the most commonly used development tool f...

Page Refactoring Skills - Javascript, CSS

About JS, CSS CSS: Stylesheet at the top Avoid CS...

VMware virtual machine installation Linux system graphic tutorial

This article shares the specific steps of VMware ...

Analyzing ab performance test results under Apache

I have always used Loadrunner to do performance t...

Teach you step by step to configure MySQL remote access

Preface When using the MySQL database, sometimes ...

Introduction to Royal Blue Color Matching for Web Design

Classical color combinations convey power and auth...

Install Zookeeper under Docker (standalone and cluster)

After starting Docker, let's take a look at t...

Web designers also need to learn web coding

Often, after a web design is completed, the desig...

Vue implements the product tab of the product details page function

This article example shares the specific code of ...

VUE render function usage and detailed explanation

Table of contents Preface The role of render Rend...

Details of MutationObServer monitoring DOM elements in JavaScript

1. Basic Use It can be instantiated through the M...