前端定时器

本文介绍前端开发中如何实现定时器功能,特别是在商品预售和众筹等场景的应用。提供了一个从实际项目中拆解出来的定时器组件示例,帮助开发者理解和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端定时器,对商品预售,众筹等功能的开发中,一定会用得到的,在这献给大家:

data(){
	return {
		d: '00', // 天的默认值
        h: '00', // 小时的默认值
        i: '00', // 分钟的默认值
        s: '00', // 秒的默认值
        timer: null ,// 定时器
        seconds: 0, // 记录不停倒计过程中变化的秒数
        timestamp:0,
	}
}
watch: {
        	// 监听时间戳的变化
        	timestamp(newVal, oldVal) {
        		// 如果倒计时间发生变化,清除定时器,重新开始倒计时
        		this.clearTimer();
        		this.start();
        	}
        },
// 倒计时
            start() {
            	// 避免可能出现的倒计时重叠情况
                const vm = this
            	this.clearTimer();
            	if (this.timestamp <= 0) return;
                console.log(this.timestamp)
            	this.seconds = Number(this.timestamp);
            	this.formatTime(this.seconds);
            	this.timer = setInterval(() => {
            		this.seconds--;
            		// 发出change事件
            		this.$emit('change', this.seconds);
            		if (this.seconds < 0) {
            			return this.end();
            		}
                    
            		this.formatTime(this.seconds);
            	}, 1000);
            },
            // 格式化时间
            formatTime(seconds) {
            	// 小于等于0的话,结束倒计时
            	seconds <= 0 && this.end();
            	let [day, hour, minute, second] = [0, 0, 0, 0];
            	day = Math.floor(seconds / (60 * 60 * 24));
            	// 判断是否显示“天”参数,如果不显示,将天部分的值,加入到小时中
            	// hour为给后面计算秒和分等用的(基于显示天的前提下计算)
            	hour = Math.floor(seconds / (60 * 60)) - day * 24;
            	// showHour为需要显示的小时
            	let showHour = null;
            	if(this.showDays) {
            		showHour = hour;
            	} else {
            		// 如果不显示天数,将“天”部分的时间折算到小时中去
            		showHour = Math.floor(seconds / (60 * 60));
            	}
            	minute = Math.floor(seconds / 60) - hour * 60 - day * 24 * 60;
            	second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
            	// 如果小于10,在前面补上一个"0"
            	showHour = showHour < 10 ? '0' + showHour : showHour;
            	minute = minute < 10 ? '0' + minute : minute;
            	second = second < 10 ? '0' + second : second;
            	day = day < 10 ? '0' + day : day;
            	this.d = day;
            	this.h = showHour;
            	this.i = minute;
            	this.s = second;
            },
            // 停止倒计时
            end() {
            	this.clearTimer();
                this._axiosOther();
            	this.$emit('end', {});
            },
            // 清除定时器
            clearTimer() {
            	if(this.timer) {
            		// 清除定时器
            		clearInterval(this.timer);
            		this.timer = null;
            	}
            }

这是我从自己封装的一个定时器的组件拆出来给大伙看的。小心了哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值