js实现更新时间执行函数

根据输入的时间,从规定的时间开始,每隔一段时间,增加时间长度。如:每隔四分钟,查询的时间段增长四分钟。

let timeStrings = ["2025-02-25 06:00:00", "2025-02-25 06:06:55"]; 
let atRegularTime = null;
/**
*@params time 设置每次增加多长的时间步数
*/
async function updateTime(time=4) {
	// 解析第二个时间为可编辑的date对象
	let dateEnd = dayjs(timeStrings[1]);
	// 修改分钟数加四
	dateEnd = dateEnd.add(time, 'minute');
	// 格式化输出新的时间串
	timeStrings[1] = dateEnd.format('YYYY-MM-DD HH:mm:ss');
	// 打印日志观察输出情况
    // console.log("Updated times:", `"${timeStrings.join(`", "`)}"`);
    // 实时更新界面时间显示情况
	time.value[1] = dayjs(timeStrings[1]);
	await fetchData(timeStrings);
}
function startAnimation() {
	if(atRegularTime){
		console.log('<======二次点击Animation=====>');
		clearInterval(atRegularTime)
	}
	console.log('<======startAnimation=====>');
	atRegularTime = setInterval(async () => {
		await updateTime();
	}, 3 * 1000);
}
function endAnimation() {
	console.log('<======endAnimation=====>');
	atRegularTime && clearInterval(atRegularTime)
}
posted @ 2025-03-13 11:52  KKKKKKYFFYFYI  阅读(16)  评论(0)    收藏  举报