JS编写一个函数,实现秒杀倒计时;要求:时间显示格式为00:00:00
时间: 2024-01-26 15:04:58 浏览: 25
以下是一个JS函数实现秒杀倒计时的例子:
```javascript
function countdown(endTime) {
var endDate = new Date(endTime);
var timer;
function updateTimer() {
var currentDate = new Date();
var remainingTime = endDate - currentDate;
if (remainingTime <= 0) {
clearInterval(timer);
console.log("秒杀已结束");
return;
}
var hours = Math.floor((remainingTime / (1000 * 60 * 60)) % 24);
var minutes = Math.floor((remainingTime / (1000 * 60)) % 60);
var seconds = Math.floor((remainingTime / 1000) % 60);
var formattedTime = formatTime(hours) + ":" + formatTime(minutes) + ":" + formatTime(seconds);
console.log(formattedTime);
}
function formatTime(time) {
return time < 10 ? "0" + time : time;
}
timer = setInterval(updateTimer, 1000);
}
// 使用示例
countdown("2022-01-01T00:00:00");
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)