JS implements countdown function based on user input minutes
No more nonsense, let me just post the code for you. The specific code is as follows:
In fact, I have come across a lot of this countdown before, but I just used other people’s source code.
In response to the needs of the project, I finally took it seriously and took a week to solve a seemingly simple problem.
The source code can be copied and pasted directly.
Redundant version + simplified version.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type="text/javascript"> var createTime = '2016-11-14 10:20:00';//开始时间 var limitTimes = 10;//时间长度 // 倒计时 入口 countdowns = window.setInterval(function(){ var arr = cutDoowns(limitTimes,createTime); document.write(formatDate(arr[0])+':'+formatDate(arr[1])+':'+formatDate(arr[2])+'</br>'); if(arr[2]){ document.write('时间到!'); } },1000); /* s,10分钟后的具体日期: date,开始时间 然后转化成毫秒比较,所得的差值化成分秒,就是倒计时的分秒。 */ function cutDoowns(s,date){ console.log(''); var flag = false; var arr = [];//arr[0]:分,arr[1]:秒,arr[2]:返回boolean var now = new Date();//当前时间 var now1 = new Date(date);//开始时间 console.log('开始时间 now1: '+now1); now1.setMinutes(now1.getMinutes()+s);//10分钟后的时间 console.log('当前时间 now :'+now); console.log('10分钟时 now1:'+now1); // 转化成年月日 时分秒 格式 var n = now.getFullYear()+'/'+(now.getMonth()+1)+'/'+now.getDay()+' '+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds(); var n1 = now1.getFullYear()+'/'+(now1.getMonth()+1)+'/'+now1.getDay()+' '+now1.getHours()+':'+now1.getMinutes()+':'+now1.getSeconds(); // 日期转化成毫秒 var time1 = (new Date(n)).getTime(); var time2 = (new Date(n1)).getTime(); // 毫秒转日期格式 var time11 = new Date(time1); var time21 = new Date(time2); console.log('当前时间:'+n+',转化成毫秒:'+time1+',time11:'+time11); console.log('10分钟时:'+n1+',转化成毫秒:'+time2+',time21:'+time21); var surplusSec = time2-time1;//距离解锁剩余毫秒 if(surplusSec<=0){ clearInterval(countdowns); flag = true; return arr = [00,00,flag]; } var minute = Math.floor(surplusSec/1000/60);//分钟 var second = Math.floor((surplusSec-minute*60*1000)/1000);//剩余秒数 console.log('剩余时间,minute: '+minute+',second: '+second+',surplusSec:'+surplusSec); // var second = Math.round(surplusTimes);//秒数 console.log('剩余时间,minute: '+minute+',second: '+second+',surplusSec:'+surplusSec); arr = [minute,second,flag]; return arr; } //格式化日期:把单字符转成双字符 function formatDate(n){ n = n.toString(); // console.log(n); if(n.length<=1){ n = '0'+n; } // console.log(n); return n; } </script> </body> </html>
Simplified version:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>打开调试工具,看效果!</title> </head> <body> <script type="text/javascript"> /* 打开调试工具,看效果! 思路: 1.设置一个倒计时的时间长度; 2.设置开始时间和当前时间; 3.结束时间是 开始时间+倒计时间; 4.结束毫秒-开始毫秒=剩余倒计时间。 */ // 准备 var countdownMinute = 10;//10分钟倒计时 var startTimes = new Date('2016-11-16 15:55');//开始时间 new Date('2016-11-16 15:21'); var endTimes = new Date(startTimes.setMinutes(startTimes.getMinutes()+countdownMinute));//结束时间 var curTimes = new Date();//当前时间 var surplusTimes = endTimes.getTime()/1000 - curTimes.getTime()/1000;//结束毫秒-开始毫秒=剩余倒计时间 // 进入倒计时 countdowns = window.setInterval(function(){ surplusTimes--; var minu = Math.floor(surplusTimes/60); var secd = Math.round(surplusTimes%60); console.log(minu+':'+secd); if(surplusTimes<=0){ console.log('时间到!'); clearInterval(countdowns); } },1000); </script> </body> </html>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP
