1、定时器

浏览器中的定时器有两种
所有的定时器都是用于在规定等待时间内执行某个方法

1)setTimeout

setTimout:只执行一次

  1. let n = 0;
  2. setTimeout(() => {
  3. console.log(++n);
  4. }, 1000);

2)setInterval

setInterval:执行多次,每隔指定的时间就执行一次

  1. let n = 0;
  2. setInterval(() => {
  3. console.log(++n);
  4. }, 1000);

3)清除定时器

  1. //=>浏览器中的定时器有两种
  2. /*
  3. 所有的定时器都是用于在规定等待时间内执行某个方法
  4. 设置定时器会有一个返回值,是个数字,代表是第几个定时器
  5. */
  6. let n = 0;
  7. //setInterval:执行多次,每隔指定的时间就执行一次
  8. let timeout = setInterval(() => {
  9. console.log(++n);
  10. if(n >= 5){
  11. //清除定时器:clearTimeout(数字) /clearInterval(数字):清除第几个定时器
  12. //使用clearTimeout也可以清除setInterval定时器
  13. clearTimeout(timeout);
  14. timeout = null;
  15. }
  16. }, 1000);
  17. let timeout2 = setTimeout(() => {
  18. }, 1000);
  19. let timeout3 = setInterval(() => {
  20. }, 1000);
  21. console.log(timeout, timeout2, timeout3);

image.png

4)定时器应用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定时器</title>
</head>
<body>
    <input type="button" value="暂停" id="handleTimer">
    <script>
        let handleTimer = document.getElementById('handleTimer'),
            timer = null,
            count = 0;

        function createTimer(){
            timer = setInterval(() =>{
                count++;
                console.log(count);
            }, 1000);
        }

        createTimer();

        handleTimer.onclick = function(){
            if(timer){
                //timer存在说明有定时器存在
                clearInterval(timer);
                timer = null;
                handleTimer.value = '开始';
                return;
            }
            //timet不存在:创建新的定时器
            createTimer();
            handleTimer.value = '暂停';
        }
    </script>
</body>
</html>

2、函数的防抖和节流重点&难点:手写实现

https://underscorejs.net/:js类库(提供很多项目中需要经常使用的方法)

1)使用underscore.js

  • 安装underscore.js

image.png

<script src="node_modules/underscore/underscore.js"></script>
    <script>
        /* 
          函数的防抖(debounce):不是在某个事件触发后就去执行函数,
          而是在指定的时间间隔内只执行一次,减少函数执行的次数。
        */
        let count = 0;
        function fn(){
            console.log(++count);
        }

        let lazyFn = _.debounce(fn, 100, true);

        window.onscroll = lazyFn;
    </script>

2)函数的防抖(debounce)

函数的防抖(debounce):不是某个事件触发就去执行函数,而是在指定的时间间隔内,执行一次,减少函数执行的次数

/* 
 * debounce:函数防抖 
 *   不是完成某个事件就去执行某函数,而是在某个间隔时间内只执行一次,避免函数的过多执行
 *   @params
 *      func:要执行的函数
 *      wait:设置的间隔等待时间
 *      immediate:设置为ture时,调用触发于开始边界而不是结束边界
 *   @return
 *      返回可被调用的函数
 * by yyrick on 2021/09/16
 */
function debounce(func, wait, immediate){
    //=>result用来存储函数执行返回的结果
    //=>timeout记录定时器
    let result = null, 
        timeout = null;

    //=>返回可被执行的函数
    return function(...args){
        //=>now记录的是事件触发的时候立即执行,还是需要等待间隔事件后执行
        let context = this,
            now = immediate && !timeout;
        //=>在设置新的定时器之前,我们要把之前设置的定时器都清理掉
        //=>因为防抖的目的是在等待时间内,只能执行一次
        clearTimeout(timeout);
        //=>设置定时器:到达时间间隔后执行函数
        timeout = setTimeout(()=>{
            if(!immediate) result = func.call(context, ...args);
            timeout = null;
        }, wait);

         //=>如果是事件触发就执行,把函数执行即可
        if(now) result = func.call(context, ...args);
        return result;
    }
}

3)函数的节流(throttle)

函数的节流(throttle):为了缩减执行频率,但不像防抖一样,一定时间内只能执行一次,而是一定时间内能执行多次

/*
* throttle:函数节流
*   是为了缩减执行频率,当达到了一定的时间间隔就会执行一次
*   @params
*      func:需要执行的函数
*      wait:设置的间隔时间
*   @return
*      返回可被调用的函数
* by yyrick on 2021/09/16
*/
let throttle = function (func, wait) {
    let timeout = null,
        result = null,
        previous = 0; //=>上次执行时间点
    return function (...args) {
      let now = new Date,
          context = this;
      //=>remaining小于等于0,表示上次执行至此所间隔时间已经超过一个时间间隔
      let remaining = wait - (now - previous);
      // console.log(remaining);
      if (remaining <= 0) {
        clearTimeout(timeout);
        previous = now;
        timeout = null;
        result = func.apply(context, args);
      } else if (!timeout) {
        timeout = setTimeout(() => {
          previous = new Date;
          timeout = null;
          result = func.apply(context, args);
        }, remaining);
      }
      return result;
    };
};

3、CSS3中的变形和动画

1)transform变形

image.png

.meimei{
    display: block;
    margin: 20px auto;
    width: 400px;
    height: 300px;
    transform: scale(.6) rotate(180deg) skew(45deg);
}
/* 
  CSS3中的变形属性:transform,改变元素的样式
  优势:开启了硬件加速,比传统的样式改变性能流畅一些
  特点:基于transform变形的元素,相当于脱离了文档流,对其余元素的位置不会产生影响
  + scale:缩放(沿着Z轴)
  + translate(X,Y) 位移  translate3d(X,Y,Z)
  + rotate(N deg) 旋转N度   rotateX/rotateY
  + skew(N deg) 倾斜N度     
  + matrix 矩阵变形         matrix3d
*/

.meimei{
    display: block;
    margin: 20px;
    width: 400px;
    height: 300px;
    /* transform: scale(.6) rotate(180deg) skew(45deg); */
    transform: translate(200px, 200px);
    z-index: -1;/* 对transform是无效的 */
}

.box{
    margin: 10px;
    width: 400px;
    height: 300px;
    background: lightblue;
}

2)过渡动画