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

Detailed steps for setting up a nexus server

1. The significance of building nexus service As ...

How to implement function currying and decurrying in Javascript

Function currying (black question mark face)? ? ?...

How to set npm to load packages from multiple package sources at the same time

Table of contents 1. Build local storage 2. Creat...

How to use MySQL group by and order by together

Suppose there is a table: reward (reward table), ...

Several ways to implement inheritance in JavaScript

Table of contents Structural inheritance (impleme...

Four completely different experiences in Apple Watch interaction design revealed

Today is still a case of Watch app design. I love...

MySQL Interview Questions: How to Set Up Hash Indexes

In addition to B-Tree indexes, MySQL also provide...

Solution to MySQL IFNULL judgment problem

Problem: The null type data returned by mybatis d...

MySQL 8.0.22 winx64 installation and configuration method graphic tutorial

The database installation tutorial of MySQL-8.0.2...

Summary of commonly used multi-table modification statements in Mysql and Oracle

I saw this question in the SQL training question ...

MySQL query syntax summary

Preface: This article mainly introduces the query...

How to use IDEA to create a web project and publish it to tomcat

Table of contents Web Development 1. Overview of ...