JavaScript implements front-end countdown effect

JavaScript implements front-end countdown effect

This article shares the specific code of JavaScript to achieve the front-end countdown effect for your reference. The specific content is as follows

Code:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    div {
      padding: 10px;
      font-size: 100px;
    }
    
    p {
      float: left;
      width: 300px;
      height: 300px;
      border: 1px solid #000000;
      color: #ffffff;
      background-color: #333333;
      text-align: center;
      line-height: 300px;
    }
  </style>
</head>

<body>
  <div>
    <p class="hour">1</p>
    <p class="minute">2</p>
    <p class="second">3</p>
  </div>
  <script>
    window.addEventListener('load', function() {
      //Get elementsvar hour = document.querySelector('.hour'); //Black box for hoursvar minute = document.querySelector('.minute'); //Black box for minutesvar second = document.querySelector('.second'); //Black box for secondsvar inputTime = +new Date('2021-2-6 18:00:00'); //Returns the total number of milliseconds of the user input timecountDown(); //Call this function once to prevent the page from being blank when it is refreshed for the first time//Start the timersetInterval(countDown, 1000);

      function countDown() {
        var nowTime = +new Date(); //Returns the total number of milliseconds of the current time var times = (inputTime - nowTime) / 1000; //tiems is the total number of milliseconds of the remaining time var h = parseInt(times / 60 / 60 % 24); //h = h < 10 ? '0' + h : h;
        hour.innerHTML = h; //Give the remaining hours to the hour black box var m = parseInt(times / 60 % 60); //minutes m = m < 10 ? '0' + m : m;
        minute.innerHTML = m;
        var s = parseInt(times % 60); //Current seconds s = s < 10 ? '0' + s : s;
        second.innerHTML = s;
      }
    })
</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:
  • JS countdown implementation code (hours, minutes, seconds)
  • JS countdown (days, hours, minutes, seconds)
  • Simple and easy to use countdown js code
  • js code to realize the 60-second countdown when clicking the button
  • 2 simple js countdown methods
  • Example of implementing a simple countdown function using native JS
  • js countdown jump example after a few seconds
  • A good js html page countdown can be accurate to seconds
  • js realizes the countdown effect of clicking to get the verification code
  • Javascript implements the countdown for product flash sales (time is synchronized with server time)

<<:  View the frequently used SQL statements in MySQL (detailed explanation)

>>:  SSH port forwarding, local port forwarding, remote port forwarding, dynamic port forwarding details

Recommend

Native js drag and drop function to create a slider example code

Drag and drop is a common function in the front e...

Example code and method of storing arrays in mysql

In many cases, arrays are often used when writing...

Implementation of MySQL's MVCC multi-version concurrency control

1 What is MVCC The full name of MVCC is: Multiver...

How to create a project with WeChat Mini Program using typescript

Create a project Create a project in WeChat Devel...

Detailed graphic explanation of mysql query control statements

mysql query control statements Field deduplicatio...

Understanding render in Vue scaffolding

In the vue scaffolding, we can see that in the ne...

Javascript to achieve the effect of closing advertisements

Here is a case study on how to close ads using Ja...

Uniapp implements DingTalk scan code login sample code

Since Uniapp does not have DingTalk authorization...

Details on using JS array methods some, every and find

Table of contents 1. some 2. every 3. find 1. som...

Steps to completely uninstall the docker image

1. docker ps -a view the running image process [r...

Vue easily realizes watermark effect

Preface: Use watermark effect in vue project, you...

Use nginx to configure domain name-based virtual hosts

1. What is a virtual host? Virtual hosts use spec...

Detailed explanation of the function and usage of DOCTYPE declaration

1. Browser rendering mode and doctype Some web pa...

Implementing circular scrolling list function based on Vue

Note: You need to give the parent container a hei...

How are Vue components parsed and rendered?

Preface This article will explain how Vue compone...