首页 > 其他 > 详细

vue防抖节流

时间:2021-08-22 14:30:59      阅读:20      评论:0      收藏:0      [点我收藏+]
<template>
 <div>
   <div class="scroll" ref="previewText" @click="fnScroll">{{count}}</div>
 </div>
</template>
<script>
 export default{
  name:‘globalHospot‘,
  data(){
    return{
      count:0,
      fnScroll:() =>{}
    }
  },
  methods: {
    fnHandleScroll (e) {
      console.log(e);
      console.log(‘scroll触发了:‘ + this.count++, new Date())
    },
    fnThrottle(fn, delay, atleast){  //节流函数
      let timer = null;
      let previous = null;
      return function(){
        let now = +new Date()
        if(!previous) previous = now;
        if(atleast && now - previous > atleast){
          fn();
          previous = now;
          clearTimeout(timer)
        }else{
          clearTimeout(timer)
          timer = setTimeout(()=>{
            fn();
            previous = null
          },delay)
        }
      }
    }
  },
  mounted() {
     this.fnScroll = this.fnThrottle(this.fnHandleScroll, 1000)  //刚创建时执行
  },
  created(){
    this.fnScroll = this.fnThrottle(this.fnHandleScroll, 1000)  //刚创建时执行
  },
}
</script>
<style lang="less">
.scroll{
  width: 300px;
border: 1px solid red;
height: 500px;
font-size: 60px;
}
</style>

vue防抖节流

原文:https://www.cnblogs.com/zjxzhj/p/15171370.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!