JavaScript - BOM和定时器的使用


一、BOM

BOM即浏览器对象模型. 它提供了独立于内容而与浏览器窗口进行交互的对象, 其核心对象是window.

BOM把浏览器当做一个对象来看待, BOM的顶级对象是window.

二、BOM的构成

window对象是浏览器的顶级对象.

window对象是JS访问浏览器窗口的一个接口

window对象是一个全局对象, 定义在全局作用域中的变量, 函数都会变成window对象的属性和方法.

三、window对象的常见事件

1. 窗口加载事件

window.onload = function() {}
或者
window.addEventListener("load", function(){});

window.onload 是窗口加载事件, 当文档内容完全加载完成会触发该事件.

有了window.onload就可以把JS代码写到页面元素的上方, 因为onload是等页面内容全部加载完毕, 再去执行处理函数.

window.onload 传统注册事件方式只能写一次, 如果有多个, 会以最后一个window.onload为准.

另一种方式

document.addEventListener('DOMContentLoaded', function(){})

DOMContentLoaded事件触发时, 仅当DOM加载完成, 不包括样式表, 图片, flash等.

2. 调整窗口大小事件

window.onresize = function(){}
window.addEventListener("resize", function(){});

window.onresize 是调整窗口大小加载事件, 当触发时就调用处理函数.

只有窗口大小发生像素变化, 就会触发这个事件.

window.innerWidth

window.innerWidth : 获取当前浏览器窗口的大小.

案例 : 根据浏览器窗口大小调整元素的显示或隐藏.

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            background-color: pink;
            margin: 0 auto;
        }
    </style>
    <script>
        window.addEventListener('DOMContentLoaded', function () {
            var div = document.querySelector('div');
            window.addEventListener('resize', function () {
                if (window.innerWidth <= 800) {
                    div.style.display = 'none';
                } else {
                    div.style.display = 'block';
                }
            })
        })
    </script>
</head>

<body>
    <div></div>
</body>

</html>

四、定时器

window对象给我们提供了2个定时器.

  1. setTimeout()
  2. setInterval()

1. setTimeout()

语法 :

window.setTimeout(调用函数, [延迟的毫秒数]);
  • 用于设置一个定时器, 该定时器在定时器到期后执行调用函数.

  • 延时时间是毫秒, 可以省略, 如果省略, 默认是0.

  • 调用函数可以直接写函数, 还可以写函数名

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // setTimeout(function () {
        //     alert('时间到');
        // }, 2000);
        function callback() {
            alert('时间到')
        }
        setTimeout(callback, 2000);
    </script>
</body>

</html>
  • 页面中可能有多个定时器, 我们需要给定时器添加名字来区分.
<body>
    <script>
        // setTimeout(function () {
        //     alert('时间到');
        // }, 2000);
        function callback() {
            alert('时间到')
        }
        var time1 = setTimeout(callback, 2000);
        var time2 = setTimeout(callback, 3000);
    </script>
</body>

回调函数

setTimeout() 这个调用函数我们也称为回调函数.

回调函数的意思就是回头调用的意思, 上一件事干完, 再回头调用这个函数.

案例 : 定时关闭广告

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 132px;
            height: 469px;
            background-image: url(./images/ad.jpg);
        }
    </style>
    <script>
        window.addEventListener('DOMContentLoaded', function () {
            var div = document.querySelector('div');
            setTimeout(function () {
                div.style.display = 'none';
            }, 5000)
        })
    </script>
</head>

<body>
    <div></div>
</body>

</html>

2. 停止setTimeout()定时器

window.clearTimeout(timeoutID)

3. setInterval()

window.setInterval(回调函数, [间隔的毫秒数]);

setInterval() 方法重复调用一个函数, 每隔这个时间, 就去调用一次回调函数.

案例 : 倒计时效果

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 190px;
            height: 260px;
            background-image: url(./images/bg.png);
            text-align: center;
        }

        .title {
            font-size: 30px;
            font-weight: 700;
            padding-top: 31px;
            color: #fff;
        }

        .desc {
            margin-top: 90px;
            font-size: 14px;
            color: #fff;
        }

        .timer {
            display: block;
            width: 130px;
            height: 30px;
            margin-top: 10px;
            margin-left: auto;
            margin-right: auto;
        }

        .timer span {
            position: relative;
            display: block;
            float: left;
            width: 30px;
            height: 30px;
            background-color: black;
            font-size: 20px;
            font-weight: 700;
            color: #fff;
            text-align: center;
            line-height: 30px;
        }

        .time_houer,
        .time_minute {
            margin-right: 20px;
        }

        .box .timer .time_houer:after {
            content: ':';
            position: absolute;
            right: -12.5px;
            font-size: 18px;
            font-weight: 700;
            color: #fff;
        }

        .box .timer .time_minute:after {
            content: ':';
            position: absolute;
            right: -12.5px;
            font-size: 18px;
            font-weight: 700;
            color: #fff;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="title">京东秒杀</div>
        <div class="desc"><strong style="font-size: 16px;">12:00</strong>点场 距结束</div>
        <span class="timer">
            <span class="time_houer"></span>
            <span class="time_minute"></span>
            <span class="time_second"></span>
        </span>
    </div>
    <script>
        var time_houer = document.querySelector('.time_houer');
        var time_minute = document.querySelector('.time_minute');
        var time_second = document.querySelector('.time_second');

        // 获取将要到达的时间
        var end = +new Date('2021-6-25 18:00:00');

        function countDown() {
            // 获取当前时间
            var start = +new Date();
            // 差值
            var diff = (end - start) / 1000;
            var h = parseInt(diff / 60 / 60 % 24);  // 获取小时
            h = h < 10 ? '0' + h : h;
            time_houer.innerHTML = h;
            var m = parseInt(diff / 60 % 60);  // 获取分钟
            m = m < 10 ? '0' + m : m;
            time_minute.innerHTML = m;
            var s = parseInt(diff % 60);  // 获取秒数
            s = s < 10 ? '0' + s : s;
            time_second.innerHTML = s;
        }
        // 先调用一次函数, 防止页面刚刷新有空白的问题.
        countDown();
        setInterval(countDown, 1000);
    </script>
</body>

</html>

4. 停止setInterval()定时器

window.clearInterval(intervalID);

clearInterval() 方法取消先前通过setInterval()建立的定时器.

案例 : 模拟发送短信

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>

<body>
    <input type="text"> <button>发送</button>
    <script>
        var btn = document.querySelector('button');
        var second = 5;
        btn.onclick = function () {
            btn.disabled = true;
            var ine = setInterval(function () {
                second--;
                btn.innerHTML = '还剩' + second + '秒再次点击';
                if (second == 0) {
                    btn.disabled = false;
                    second = 5;
                    btn.innerHTML = '发送';
                    window.clearInterval(ine);
                }
            }, 1000)
        }
    </script>
</body>

</html>

五、this

this的指向在函数定义的时候是确定不了的, 只有函数执行的时候才能确

定this到底指向谁, 一般情况下this的最终指向的是那个调用它的对象

六、this指向的几种情况

  1. 全局作用域或者普通函数中this指向全局对象window
<script>
    console.log(this);  // Window
    function fn() {
        console.log(this);
    }
    // window.fn();
    fn();  // Window
    window.setTimeout(function () {
        console.log(this);  // Window
    }, 1000)
</script>
  1. 方法调用中谁调用this指向谁
<body>
    <button>点击按钮</button>
    <script>
        var o = {
            sayHi: function () {
                console.log(this);  // o
            }
        } 
        o.sayHi();

        // this指向的是调用者
        var btn = document.querySelector('button');
        btn.onclick = function () {
            console.log(this);  // <button>点击按钮</button>
        }
    </script>
</body>
  1. 构造函数中this指向构造函数的实例.
<body>
    <button>点击按钮</button>
    <script>
        function Fun() {
            console.log(this);  // Fun
        }
        var fun = new Fun();
    </script>
</body>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值