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

如何创建支持用户输入的员工任务倒计时计时器

员工任务倒计时管理系统完整实现方案

嘿,作为网页开发新手遇到这些功能实现的困扰太正常了!我给你整理了一个包含所有需求的完整解决方案,代码里都加了详细注释,你可以直接运行测试,之后再慢慢拆解理解每个部分的逻辑~

功能说明

这个实现包含你需要的所有核心功能:

  • 管理人员输入员工姓名和指定时长,添加任务
  • 自动将员工添加到下拉菜单选项中
  • 每个员工的计时器独立倒计时,归零时自动变红
  • 支持添加多名员工,各自的计时器互不干扰

完整代码实现

HTML 结构

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>员工任务计时器</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <h2>员工任务分配</h2>
        <!-- 任务输入表单 -->
        <div class="task-form">
            <label for="employeeName">员工姓名:</label>
            <input type="text" id="employeeName" placeholder="输入员工姓名">
            
            <label for="taskDuration">任务时长(分钟):</label>
            <input type="number" id="taskDuration" min="1" placeholder="输入时长">
            
            <button id="addTaskBtn">添加任务</button>
        </div>

        <!-- 员工下拉菜单 -->
        <div class="employee-select">
            <label for="employeeList">已添加员工:</label>
            <select id="employeeList">
                <option value="">请选择员工</option>
            </select>
        </div>

        <!-- 计时器展示区域 -->
        <div class="timers-container" id="timersContainer">
            <!-- 动态生成的员工计时器会在这里插入 -->
        </div>
    </div>

    <script src="script.js"></script>
</body>
</html>

CSS 样式

.container {
    max-width: 800px;
    margin: 20px auto;
    padding: 20px;
    font-family: Arial, sans-serif;
}

.task-form {
    margin-bottom: 20px;
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

.task-form input {
    padding: 8px;
    font-size: 14px;
}

.task-form button {
    padding: 8px 16px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.task-form button:hover {
    background-color: #45a049;
}

.employee-select {
    margin-bottom: 20px;
}

.timer-card {
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.timer-red {
    color: red;
    font-weight: bold;
    font-size: 1.2em;
}

JavaScript 逻辑

// 存储所有员工计时器的数组,用于管理多个计时器
const employeeTimers = [];

// 获取DOM元素
const employeeNameInput = document.getElementById('employeeName');
const taskDurationInput = document.getElementById('taskDuration');
const addTaskBtn = document.getElementById('addTaskBtn');
const employeeListSelect = document.getElementById('employeeList');
const timersContainer = document.getElementById('timersContainer');

// 监听添加任务按钮点击事件
addTaskBtn.addEventListener('click', addEmployeeTask);

function addEmployeeTask() {
    const name = employeeNameInput.value.trim();
    const durationMinutes = parseInt(taskDurationInput.value);

    // 验证输入是否有效
    if (!name || isNaN(durationMinutes) || durationMinutes <= 0) {
        alert('请输入有效的员工姓名和时长(时长必须大于0)');
        return;
    }

    // 检查是否已添加过该员工(避免重复)
    const isEmployeeExists = employeeTimers.some(timer => timer.name === name);
    if (isEmployeeExists) {
        alert('该员工已存在,请添加其他员工或修改任务时长');
        return;
    }

    // 将员工添加到下拉菜单
    addEmployeeToSelect(name);

    // 创建员工计时器并添加到容器
    createEmployeeTimer(name, durationMinutes);

    // 清空输入框
    employeeNameInput.value = '';
    taskDurationInput.value = '';
}

// 向下拉菜单添加员工选项
function addEmployeeToSelect(name) {
    const option = document.createElement('option');
    option.value = name;
    option.textContent = name;
    employeeListSelect.appendChild(option);
}

// 创建单个员工的计时器
function createEmployeeTimer(name, durationMinutes) {
    // 计算总秒数
    let totalSeconds = durationMinutes * 60;

    // 创建计时器DOM元素
    const timerCard = document.createElement('div');
    timerCard.className = 'timer-card';
    timerCard.innerHTML = `
        <div>
            <h3>${name}</h3>
            <p>剩余时间:<span class="timer-display" id="timer-${name}">${formatTime(totalSeconds)}</span></p>
        </div>
        <button class="stop-btn" data-name="${name}">停止计时</button>
    `;

    timersContainer.appendChild(timerCard);

    // 启动倒计时
    const timerInterval = setInterval(() => {
        totalSeconds--;
        const timerDisplay = document.getElementById(`timer-${name}`);
        timerDisplay.textContent = formatTime(totalSeconds);

        // 当时间归零时,添加红色样式并停止计时
        if (totalSeconds <= 0) {
            clearInterval(timerInterval);
            timerDisplay.classList.add('timer-red');
            timerDisplay.textContent = '时间已到!';
        }
    }, 1000);

    // 存储计时器信息,用于后续管理(比如停止计时)
    employeeTimers.push({
        name,
        intervalId: timerInterval
    });

    // 监听停止计时按钮点击
    const stopBtn = timerCard.querySelector('.stop-btn');
    stopBtn.addEventListener('click', () => stopTimer(name));
}

// 格式化时间为 分:秒 的格式
function formatTime(seconds) {
    const mins = Math.floor(seconds / 60);
    const secs = seconds % 60;
    return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}

// 停止指定员工的计时器
function stopTimer(name) {
    const timer = employeeTimers.find(t => t.name === name);
    if (timer) {
        clearInterval(timer.intervalId);
        const timerDisplay = document.getElementById(`timer-${name}`);
        timerDisplay.textContent = '已停止';
    }
}

关键功能解释

  1. 向下拉菜单添加选项:通过createElement('option')创建新选项元素,然后用appendChild添加到下拉菜单中,每次添加新员工时自动调用这个函数。
  2. 多员工计时器管理:用employeeTimers数组存储每个员工的计时器ID,这样可以独立控制每个计时器的启动和停止,互不干扰。
  3. 倒计时归零时变红:当totalSeconds小于等于0时,给时间显示元素添加timer-red CSS类,这个类在样式里定义了红色和加粗效果。
  4. 输入验证:添加任务时会检查姓名是否为空、时长是否有效,还会避免重复添加同一员工,提升用户体验。

你可以把这三个文件(HTML、CSS、JS)放在同一个文件夹里,直接打开HTML文件就能运行测试啦!如果有某个部分看不懂,随时可以拆解代码慢慢琢磨~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:15:15