JavaScript第六天 定时器与函数封装

本文探讨JavaScript中的定时器,包括setInterval用于重复执行任务,setTimeout进行延迟单次执行,以及如何使用clearInterval和clearTimeout来清除定时器。特别地,文章结合实例介绍了在轮播图中的应用。

定时器

分为两类
setIntervar
重复执行或者叫间歇执行,隔某个时间就执行一次。
srtTimeout
延迟执行,延迟某个特定的时间开始执行,只执行一次。

清除定时器
clearInterval(intervalId)
clearTimeout(intervalId)

轮播图

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        a {
            text-decoration: none;
        }

        img {
            width: 500px;
            height: 400px;
            float: left;
        }

        .box {
            text-align: center;
            position: relative;
            left: 200px;
            width: 500px;
            height: 400px;
            overflow: hidden;
        }

        #top {
            position: absolute;
            width: 500px;
            background-color: rgba(0, 0, 0, .2);
            font-size: 20px;
            color: #fff;
            top: 0;
            left: 0;
            margin: 0 auto;
        }

        .left {
            display: inline-block;
            position: absolute;
            top: 180px;
            left: 0px;
            width: 30px;
            height: 50px;
            color: black;
            background-color: rgba(0, 0, 0, .5);
        }

        .right {
            display: inline-block;
            position: absolute;
            color: black;
            top: 180px;
            right: 0px;
            width: 30px;
            height: 50px;
            background-color: rgba(0, 0, 0, .5);
        }

        .down {

            position: absolute;
            width: 500px;
            background-color: rgba(0, 0, 0, .2);
            font-size: 20px;
            color: #fff;
            bottom: 0;
            left: 0;
            margin: 0 auto;
        }

        .yuan {
            position: absolute;
            bottom: 25px;
            width: 500px;
            margin: 0 auto;
        }

        p span {
            display: inline-block;

            border: 10px solid white;
            border-radius: 50%;
        }

        .red {
            border-color: #f00;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="../img/1.jpg" alt="" id="tu">

        <p id="top">1/4</p>
        <button class="left">
            < </button> <button class="right"> >
        </button>
        <p class="yuan">
            <span class="red"></span>
            <span></span>
            <span></span>
            <span></span>
        </p>
        <p class="down">风景1</p>
    </div>

    <script>

        var image = document.getElementsByTagName("img")[0];
        var sp = document.getElementsByTagName("span");
        var top1 = document.getElementById("top");
        var down = document.getElementsByClassName("down")[0];
        var tu = document.getElementById("tu")
        tu.onmouseover=function(){
            clearInterval(timer)
        }
        tu.onmouseout=function(){
            timer=setInterval(autonext, 1000)
        }
        // var n = 1;
        var timer = setInterval(autonext, 1000);
        function autonext() {
            a++;
            if (a > 4) {
                a = 1;
            }
            tu.src = "../img/" + a + ".jpg";
            top1.innerHTML = a + "/4";
            down.innerHTML = "风景" + a
            for (var i = 0; i < sp.length; i++) {
                sp[i].className = ""
            }
            sp[a - 1].className = "red"
        }
        // 圆圈
        for (var i = 0; i < sp.length; i++) {
            sp[i].index = i
            sp[i].onmouseover = function () {
                for (var i = 0; i < sp.length; i++) {
                    sp[i].className = "";

                }
                a = this.index + 1
                this.className = "red";
                image.src = "../img/" + (this.index + 1) + ".jpg";
                top1.innerHTML = this.index + 1 + "/4";
                down.innerHTML = "风景" + (this.index + 1);

            }
        }
        // 按钮

        var left = document.getElementsByClassName("left")[0];
        var right = document.getElementsByClassName("right")[0];
        var a = 1;

        left.onclick = function () {
            a--;
            if (a < 1) {
                a = 4;
            }
            tu.src = "../img/" + a + ".jpg";
            top1.innerHTML = a + "/4";
            down.innerHTML = "风景" + a
            for (var i = 0; i < sp.length; i++) {
                sp[i].className = "";

            }
            sp[a - 1].className = "red"
        }
        right.onclick = function () {
            a++;
            if (a > 4) {
                a = 1;
            }
            tu.src = "../img/" + a + ".jpg";
            top1.innerHTML = a + "/4";
            down.innerHTML = "风景" + a
            for (var i = 0; i < sp.length; i++) {
                sp[i].className = ""
            }
            sp[a - 1].className = "red"

        }

    </script>
</body>

</html>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值