web前端-homework js随机点名&秒表计时器

一、结合抽奖案例完成随机点名程序,要求如下:

1.点击点名按钮,名字界面随机显示,按钮文字由点名变为停止

2.再次点击点名按钮,显示当前被点名学生姓名,按钮文字由停止变为点名

3.样式请参考css及html自由发挥完成。

html源码:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>随机点名</title>
    <style>
        .container{
            width: 500px;
            height: 300px;
            border: 1px dashed #333;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
            padding-top: 50px;
        }
        .name-display {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 100px;
        }
        #start-stop-btn {
            width: 150px;
            height: 50px;
            background-color: #3498db;
            border: none;
            color: #fff;
            font-size: 18px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="name-display" id="nameDisplay">点击开始点名</div>
        <button id="start-stop-btn">开始点名</button>
    </div>

    <script>
        var students = ["张三", "李四", "王五"];
        var nameDisplay = document.getElementById("nameDisplay");
        var startStopBtn = document.getElementById("start-stop-btn");
        var flag = false;
        var timer;

        startStopBtn.onclick = function() {
            if (!flag) {
                // 将标志变量置为true,表示进入点名状态
                flag = true;
                startStopBtn.innerHTML = "停止点名";
                // 启动定时器,每100ms随机选取一个名字显示
                timer = setInterval(pickRandomName, 100);
            } else {
                // 将标志变量置为false,表示停止点名
                flag = false;
                startStopBtn.innerHTML = "开始点名";
                // 清除定时器,停止随机选取名字
                clearInterval(timer);
            }
        };
        
        // 随机选取名字的函数
        function pickRandomName() {
            var randomIndex = Math.floor(Math.random() * students.length);
            var randomName = students[randomIndex];
            nameDisplay.textContent = randomName;
        }
    </script>
</body>
</html>

效果:

二、使用js及html、css完成秒表计时器,要求如下:

1.界面为一个显示计时面板和三个按钮分别为:开始,暂停,重置

2.点击开始,面板开始计时,

3.点击暂停,面板停止

4.点击重置,计时面板重新为0

提示:采用定时器及定义计数器变量完成,定时器间隔为1s

html源码:

js 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>秒表计时器</title>
    <style>
        .container{
            width: 600px;
            height: 300px;
            border: 1px dashed #333;
            position: absolute;
            left: 50%;
            top: 50%;
            text-align: center;
            transform: translate(-50%, -50%);
            line-height: 100px;
        }
        #display {
            font-size: 36px;
            font-weight: bold;
            margin-top: 20px;
            margin-bottom: 20px;
        }
        button {
            width: 80px;
            height: 40px;
            margin: 5px;
            font-size: 16px;
            background-color: #3498db;
            border: none;
            color: #fff;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="display">00:00:00</div>
        <button id="start">开始</button>
        <button id="pause">暂停</button>
        <button id="reset">重置</button>
    </div>

    <script>
        var timer;
        var isRunning = false;
        var seconds = 0, minutes = 0, hours = 0;

        function startTimer() {
            isRunning = true;
             // 每秒更新一次时间
            timer = setInterval(updateTime, 1000);
        }
        
        // 暂停计时器
        function pauseTimer() {
            isRunning = false;
            clearInterval(timer);
        }
        
        // 重置时间
        function resetTimer() {
            isRunning = false;
            clearInterval(timer);
            seconds = 0;
            minutes = 0;
            hours = 0;
            updateDisplay();
        }
        
        // 更新时间
        function updateTime() {
            seconds++;
            if (seconds === 60) {
                seconds = 0;
                minutes++;
                if (minutes === 60) {
                    minutes = 0;
                    hours++;
                }
            }
            updateDisplay();
        }

        // 更新显示时间
        function updateDisplay() {
            var display = document.getElementById('display');
            // 设置显示内容
            display.textContent = pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
        }

        function pad(val) {
            // 如果val大于9,返回val,否则返回'0' + val
            return val > 9 ? val : '0' + val;
        }

        document.getElementById('start').addEventListener('click', function() {
            if (!isRunning) {
                startTimer();
            }
        });

        document.getElementById('pause').addEventListener('click', function() {
            if (isRunning) {
                pauseTimer();
            }
        });

        document.getElementById('reset').addEventListener('click', function() {
            resetTimer();
        });
    </script>
</body>
</html>

效果:

相关推荐
木斯佳20 分钟前
前端八股文面经大全:bilibili生态技术方向二面 (2026-03-25)·面经深度解析
前端·ai·ssd·sse·rag
不会写DN30 分钟前
Gin 日志体系详解
前端·javascript·gin
冬夜戏雪1 小时前
实习面经记录(十)
java·前端·javascript
爱学习的程序媛2 小时前
【Web前端】JavaScript设计模式全解析
前端·javascript·设计模式·web
小码哥_常2 小时前
从SharedPreferences到DataStore:Android存储进化之路
前端
老黑2 小时前
开源工具 AIDA:给 AI 辅助开发加一个数据采集层,让 AI 从错误中自动学习(Glama 3A 认证)
前端·react.js·ai·nodejs·cursor·vibe coding·claude code
jessecyj3 小时前
Spring boot整合quartz方法
java·前端·spring boot
苦瓜小生3 小时前
【前端】|【js手撕】经典高频面试题:手写实现function.call、apply、bind
java·前端·javascript
天若有情6733 小时前
前端HTML精讲03:页面性能优化+懒加载,搞定首屏加速
前端·性能优化·html
踩着两条虫3 小时前
AI驱动的Vue3应用开发平台深入探究(十):物料系统之内置组件库
android·前端·vue.js·人工智能·低代码·系统架构·rxjava