Article cover

随机点名练习

案例

现有马超、黄忠、赵云、关羽、张飞等人,点击开始按钮随机抽取数组的一个数据,放到页面中,点击结束按钮删除数组当前抽取的一个数据,当抽取到最后一个数据的时候,两个按钮同时禁用(写点开始里面,只剩最后一个数据不用抽了)

核心:利用定时器快速展示,停止定时器结束展示

代码实现

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        h2 {
            text-align: center;
        }

        .box {
            width: 600px;
            margin: 50px auto;
            display: flex;
            font-size: 25px;
            line-height: 40px;
        }

        .qs {

            width: 450px;
            height: 40px;
            color: red;

        }

        .btns {
            text-align: center;
        }

        .btns button {
            width: 120px;
            height: 35px;
            margin: 0 50px;
        }
    </style>
</head>

<body>
    <h2>随机点名</h2>
    <div class="box">
        <span>名字是:</span>
        <div class="qs">这里显示姓名</div>
    </div>
    <div class="btns">
        <button class="start">开始</button>
        <button class="end">结束</button>
    </div>

    <script>
        // 数据数组
        const arr = ['马超', '黄忠', '赵云', '关羽', '张飞']
        // 定时器的全局变量
        let timerId = 0
        // 随机号要全局变量
        let random = 0
        // 业务1.开始按钮模块
        const qs = document.querySelector('.qs')
        // 1.1 获取开始按钮对象
        const start = document.querySelector('.start')
        // 1.2 添加点击事件
        start.addEventListener('click', function () {
            timerId = setInterval(function () {
                // 随机数
                random = parseInt(Math.random() * arr.length)
                // console.log(arr[random])
                qs.innerHTML = arr[random]
            }, 35)
            // 如果数组里面只有一个值了,还需要抽取吗?  不需要  让两个按钮禁用就可以
            if (arr.length === 1) {
                // start.disabled = true
                // end.disabled = true
                start.disabled = end.disabled = true
            }
        })

        // 2. 关闭按钮模块
        const end = document.querySelector('.end')
        end.addEventListener('click', function () {
            clearInterval(timerId)
            // 结束了,可以删除掉当前抽取的那个数组元素
            arr.splice(random, 1)
            console.log(arr)
        })
    </script>
</body>
</html>

扩展

由于快速点击开始有bug,所以在鼠标点击开始事件后开始按钮被禁用,只剩最后一人时两个按钮都被禁用。代码实现如下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        h2 {
            text-align: center;
        }

        .box {
            width: 600px;
            margin: 50px auto;
            display: flex;
            font-size: 25px;
            line-height: 40px;
        }

        .qs {

            width: 450px;
            height: 40px;
            color: red;

        }

        .btns {
            text-align: center;
        }

        .btns button {
            width: 120px;
            height: 35px;
            margin: 0 50px;
        }
    </style>
</head>

<body>
    <h2>随机点名</h2>
    <div class="box">
        <span>名字是:</span>
        <div class="qs">这里显示姓名</div>
    </div>
    <div class="btns">
        <button class="start">开始</button>
        <button class="end">结束</button>
    </div>

    <script>
        // 数据数组
        const arr = ['马超', '黄忠', '赵云', '关羽', '张飞']
        // 全局声明变量
        let timerId = 0
        let random = 0
        // 获取对象
        const qs = document.querySelector('.qs')
        const start = document.querySelector('.start')
        // 添加事件
        start.addEventListener('click', function () {
            timerId = setInterval(function () {
                const random = Math.floor(Math.random() * arr.length)
                qs.innerHTML = arr[random]
                start.disabled = true
            }, 35)
            if (arr.length === 1) {
                start.disabled = end.disabled = true
            }
        })
        const end = document.querySelector('.end')
        end.addEventListener('click', function () {
            clearInterval(timerId)
            arr.splice(random, 1)
            start.disabled = false
        })

    </script>
</body>

</html>

Comments

Contents

404NotFound

Keyboard shortcuts

Ctrl/⌘+F
Open site search; press again while open for browser Find. Editing areas keep their original behavior.
Shift+D
Toggle appearance, except while editing text.
Esc
Close a theme menu or dismissible overlay.
↑ ↓
Select the previous or next quick action.
← →
Move between the toolbar icons.
Home End
Select the first or last quick action.
Enter Space
Run the selected action.
Tab
Leave the quick menu and continue through the page.

Hold Shift while right-clicking to keep the native menu.

Copy page link