You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript实现飞行娱乐系统指定时间自动重置倒计时功能

飞行娱乐系统剩余时长倒计时实现方案

基础HTML结构

保留你已有的静态结构即可:

<div class="flighttime">
  3H 20M
</div>

JavaScript 核心逻辑

这段代码会自动解析初始时间、启动倒计时,归零后自动重置:

// 获取页面上的倒计时元素
const timerElement = document.querySelector('.flighttime');

// 解析时间字符串(如"3H 20M")为总秒数
function convertToSeconds(timeString) {
  const hourMatch = timeString.match(/(\d+)H/);
  const minuteMatch = timeString.match(/(\d+)M/);
  const hours = hourMatch ? parseInt(hourMatch[1]) : 0;
  const minutes = minuteMatch ? parseInt(minuteMatch[1]) : 0;
  return hours * 3600 + minutes * 60;
}

// 初始化总秒数,同时保存初始值用于重置
let totalSeconds = convertToSeconds(timerElement.textContent.trim());
const initialTotal = totalSeconds;

// 更新倒计时显示内容
function updateDisplay() {
  const hours = Math.floor(totalSeconds / 3600);
  const minutes = Math.floor((totalSeconds % 3600) / 60);
  timerElement.textContent = `${hours}H ${minutes}M`;

  totalSeconds--;
  // 倒计时归零时重置为初始时间
  if (totalSeconds < 0) {
    totalSeconds = initialTotal;
  }
}

// 页面加载后立即渲染一次,之后每秒更新
updateDisplay();
setInterval(updateDisplay, 1000);

可选美化CSS

如果要匹配附图右上角的视觉效果,可以添加以下样式:

.flighttime {
  font-family: 'Helvetica Neue', sans-serif;
  font-size: 1.3rem;
  font-weight: 600;
  color: #ffffff;
  background: rgba(0, 0, 0, 0.4);
  padding: 6px 10px;
  border-radius: 3px;
  letter-spacing: 0.5px;
}

内容的提问来源于stack exchange,提问作者Gavin Walsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 19:18:37