jQuery实现倒计时功能

编程知识 行业动态 更新时间:2024-06-13 00:21:04

首先计算倒计时的总秒数,然后定义一个定时器let timer = null

var intDiff = parseInt(60);//倒计时总秒数量
timer = window.setInterval(function() {
	var day = 0,
		hour = 0,
		minute = 0,
		second = 0; //时间默认值
	if (intDiff > 0) {
		day = Math.floor(intDiff / (60 * 60 * 24));
        hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
	    minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
	    second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
	}
	if (hour <= 9)
		hour = '0' + hour; //小于9的时候给小时添加一个0例如00:13:14
	if (minute <= 9)
		minute = '0' + minute;
	if (second <= 9)
		second = '0' + second;
	if (Number(hour) == 0 && Number(minute) == 0 & Number(second) == 0){
	    clearInterval(timer); //时分秒均为0的时候清除定时器	
		$("#timer span:eq(0)").text(hour); //获取页面的时标签并更改为现在的
		$("#timer span:eq(1)").text(minute);
		$("#timer span:eq(2)").text(second);	
		intDiff--; // 秒数-1
}, 1000);

更多推荐

jQuery实现倒计时功能

本文发布于:2023-03-31 02:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/113df753483a0da3f0e0b7f0de95225c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:倒计时   功能   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!