您的位置:首页 > 文旅 > 旅游 > 做一个英文网站_页面设计常用的字体颜色有_专业seo整站优化_seo长尾关键词排名

做一个英文网站_页面设计常用的字体颜色有_专业seo整站优化_seo长尾关键词排名

2025/2/24 5:46:12 来源:https://blog.csdn.net/m0_64150479/article/details/143468354  浏览:    关键词:做一个英文网站_页面设计常用的字体颜色有_专业seo整站优化_seo长尾关键词排名
做一个英文网站_页面设计常用的字体颜色有_专业seo整站优化_seo长尾关键词排名

目录

  • 防抖源码及使用
  • 节流源码及使用
    • 方法一:时间戳
    • 方法二:定时器

防抖源码及使用

  <button class="btn">点击发送消息</button><!-- 防抖:就是要延迟执行,你一直操作触发事件一直不执行处理函数,只有当你停止操作等待多少秒后才执行 --><script>function debounce(func, wait) {let timer = nullreturn function(...args) {if(timer) {clearInterval(timer)timer = null}timer =  setTimeout(() => {func.apply(this, args)}, wait)}}function sendMessage(name) {console.log(`${name}, i love you`)}const btn = document.querySelector('.btn')const sendMsgDebounce = debounce(sendMessage, 3000)btn.addEventListener('click', () => {sendMsgDebounce('lisi')})

节流源码及使用

方法一:时间戳

  <button class="btn">点击发送消息</button><script>// 节流:固定时间内只执行一次// 方法一:时间戳function throttle(func, wait) {let preTime = 0return function(...args) {if(new Date() - preTime > wait) {// 当 n 秒内不重复执行func.apply(this, args)preTime = new Date()}}}function sendMessage(name) {console.log(`${name}, i love you`)}const btn = document.querySelector('.btn')const sendMsgDebounce = throttle(sendMessage, 3000)btn.addEventListener('click', () => {sendMsgDebounce('lisi')})</script>

方法二:定时器

  <button class="btn">点击发送消息</button><script>// 节流:固定时间内只执行一次// 方法二:定时器function throttle(func, wait) {let timer = nullreturn function(...args) {if(!timer) {timer = setTimeout(() => {timer = nullfunc.apply(this, args)}, wait)}}}function sendMessage(name) {console.log(`${name}, i love you`)}const btn = document.querySelector('.btn')const sendMsgDebounce = throttle(sendMessage, 3000)btn.addEventListener('click', () => {sendMsgDebounce('lisi')})</script>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com