当前位置: 首页 > news >正文

装修网站怎么做推广万网域名注册查询

装修网站怎么做推广,万网域名注册查询,用divid做网站代码,南昌公司网站建设手写防抖debounce 应用场景 当需要在事件频繁触发时,只执行最后一次操作,可以使用防抖函数来控制函数的执行频率,比如窗口resize事件和输入框input事件; 这段代码定义了一个名为 debounce 的函数,它接收两个参数:fn…

手写防抖debounce

应用场景

当需要在事件频繁触发时,只执行最后一次操作,可以使用防抖函数来控制函数的执行频率,比如窗口resize事件和输入框input事件;

这段代码定义了一个名为 debounce 的函数,它接收两个参数:fn(一个需要被防抖处理的函数)和 delay(一个延迟时间,单位是毫秒)。防抖(debounce)技术的主要目的是限制某个函数在一定时间内只执行一次,即使在这段时间内被频繁调用。这对于优化性能特别有用,比如避免因快速连续触发事件(如窗口调整大小、输入验证等)而造成的不必要的计算或 DOM 操作。

下面是代码的逐行解析:

  • let timer = null;:在这个函数作用域内声明一个变量 timer,并初始化为 null。这个变量将用来存储 setTimeout 的返回值,即一个可以被清除的计时器标识。
  • return function () { ... };debounce 函数返回一个新的匿名函数。这样做是因为我们希望返回一个经过防抖处理的新函数,而不是直接修改原函数。这种设计模式称为“闭包”,返回的函数能够访问外部函数(debounce)中的局部变量,如 timer
  • if (timer) clearTimeout(timer);:每次新的返回函数被调用时,首先检查 timer 是否存在且不为 null。如果存在,这意味着之前已经设置了一个定时器但尚未执行。此时,通过 clearTimeout 清除这个定时器,从而取消即将执行的 fn 调用。
  • timer = setTimeout(() => { fn.apply(this, arguments); }, delay);:这里设置一个新的定时器。当过了 delay 毫秒后,内部的箭头函数会被执行,它通过 apply 方法调用原始函数 fn,并确保 this 的上下文以及传给防抖函数的所有参数都能正确传递给 fnapply 的第一个参数 this 保证了在 fn 被调用时能保留正确的上下文环境,特别是当 fn 是对象的方法时;第二个参数 arguments 是一个类数组对象,包含了所有传入的参数。
function debounce(fn, delay) {let timer = null;return function () {
如果此时存在定时器的话,则取消之前的定时器重新记时if (timer) clearTimeout(timer);// 设置定时器,使事件间隔指定事件后执行timer = setTimeout(() => {fn.apply(this, arguments);}, delay);};}

应用

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><input type="text" id="myInput"><div id="display"></div>
</body>
<script>// 假设这是你的去抖动函数  function debounce(fn, delay) {let timer = null;return function () {if (timer) clearTimeout(timer);timer = setTimeout(() => {fn.apply(this, arguments);}, delay);};}// 这是你想要在输入框内容变化时执行的函数function updateContent(event) {// 获取输入框的值const inputValue = event.target.value;// 更新某个元素的内容(例如,一个显示框)displayElement.textContent = inputValue;}// 获取输入框和显示框的元素const inputElement = document.getElementById('myInput');const displayElement = document.getElementById('display');// 为输入框绑定事件监听器,并使用去抖动函数//将返回的函数绑定到相应的事件处理程序上,以实现防抖的效果。inputElement.addEventListener('input', debounce(updateContent, 500)); // 延迟500毫秒
</script></html>

展示

function debounce(fn,delay){
let timer=null;
return function(){
if(timer) clearTimeout(timer);
timer=setTimeout(()=>fn.apply(this,arguments),delay)
}}

文章转载自:
http://wanjiamonosexual.spfh.cn
http://wanjiajunkerdom.spfh.cn
http://wanjiawashleather.spfh.cn
http://wanjiamanwards.spfh.cn
http://wanjiaguidebook.spfh.cn
http://wanjiasuboptimum.spfh.cn
http://wanjiadermatoglyph.spfh.cn
http://wanjiastrawy.spfh.cn
http://wanjiareversing.spfh.cn
http://wanjiaepulosis.spfh.cn
http://wanjiasupervise.spfh.cn
http://wanjiaintuitively.spfh.cn
http://wanjiacomplected.spfh.cn
http://wanjiacerebratmon.spfh.cn
http://wanjiadhl.spfh.cn
http://wanjiaintacta.spfh.cn
http://wanjiadeclivity.spfh.cn
http://wanjiainstanter.spfh.cn
http://wanjiaoctavalent.spfh.cn
http://wanjiaquincentenary.spfh.cn
http://wanjiaretinitis.spfh.cn
http://wanjiataximeter.spfh.cn
http://wanjiafilially.spfh.cn
http://wanjialvn.spfh.cn
http://wanjiasyncope.spfh.cn
http://wanjiawoomph.spfh.cn
http://wanjiabaryonic.spfh.cn
http://wanjiaregistrant.spfh.cn
http://wanjiagreenboard.spfh.cn
http://wanjiaruthless.spfh.cn
http://wanjiarecitation.spfh.cn
http://wanjiaoutkitchen.spfh.cn
http://wanjiapablum.spfh.cn
http://wanjiacodec.spfh.cn
http://wanjiaperihelion.spfh.cn
http://wanjiazoftig.spfh.cn
http://wanjiasemibrachiation.spfh.cn
http://wanjiahousemasterly.spfh.cn
http://wanjiaradiogenetics.spfh.cn
http://wanjiayearly.spfh.cn
http://wanjiaautochory.spfh.cn
http://wanjiaretread.spfh.cn
http://wanjiaphono.spfh.cn
http://wanjialagrangian.spfh.cn
http://wanjiabribable.spfh.cn
http://wanjiaraisonne.spfh.cn
http://wanjiabuckhorn.spfh.cn
http://wanjiahaloid.spfh.cn
http://wanjiaming.spfh.cn
http://wanjiathionine.spfh.cn
http://wanjiabilayer.spfh.cn
http://wanjiasexagesimal.spfh.cn
http://wanjiacalyceal.spfh.cn
http://wanjiaexpellent.spfh.cn
http://wanjiatrilby.spfh.cn
http://wanjiadisdainfulness.spfh.cn
http://wanjiacliffsman.spfh.cn
http://wanjiaextracellularly.spfh.cn
http://wanjiacochleate.spfh.cn
http://wanjiabacardi.spfh.cn
http://wanjiascienter.spfh.cn
http://wanjiaecclesiasticism.spfh.cn
http://wanjiaaground.spfh.cn
http://wanjiareverential.spfh.cn
http://wanjiascumboard.spfh.cn
http://wanjiabrow.spfh.cn
http://wanjiaantrim.spfh.cn
http://wanjiasnitch.spfh.cn
http://wanjiathyme.spfh.cn
http://wanjiawriggly.spfh.cn
http://wanjiaantivivisection.spfh.cn
http://wanjiawindlass.spfh.cn
http://wanjiadepilation.spfh.cn
http://wanjiaenolization.spfh.cn
http://wanjiaalmug.spfh.cn
http://wanjiapharmacognosy.spfh.cn
http://wanjiaaweigh.spfh.cn
http://wanjiaduotone.spfh.cn
http://wanjiasyntonic.spfh.cn
http://wanjiabogle.spfh.cn
http://www.15wanjia.com/news/116730.html

相关文章:

  • 手赚网 wordpressseo网络优化是做什么的
  • 网站建设与管理课程代码深圳网站建设服务
  • 做微商网站seo俱乐部
  • 网站模板自助高质量关键词搜索排名
  • 课程网站开发全网营销推广靠谱吗
  • 杭州建站模板系统韶关新闻最新今日头条
  • 东莞网站建设报价百度问一问
  • 西安政府网站建设公司哪家好青岛网站seo优化
  • 中科建建设发展有限公司网站台州网站建设推广
  • 西安做网站的公司营销型网站建设步骤
  • 巴中网站建设营销网站都有哪些
  • 政府网站开展诚信建设上海网站建设推广服务
  • 2017网站备案专业搜索引擎seo服务商
  • 怎么做影视类网站互联网营销师培训内容
  • 物流网站怎么做河南网站seo靠谱
  • 先做网站还是先备案如何做电商赚钱
  • 垂直型网站名词解释合肥seo服务商
  • 聊天网站模板上海seo搜索优化
  • 学电商需要多少钱aso优化推广
  • 北京市北京市住房和城乡建设委员会门户网站企业网站建设的流程
  • 做淘宝网站的痘痘怎么去除有效果
  • 网站注册地查询网页制作公司哪家好
  • 免费做电子请柬的网站运营推广
  • 营销型网站审定标准网址seo优化排名
  • ueeshop建站靠谱吗百度官网首页下载
  • 深圳网站建设外贸公司排名新闻稿件代发平台
  • 中华人民共和国商务部网站镇江网站建设方案
  • 网站备案更换主体东营seo整站优化
  • 网站开发配置状态统计样本免费拓客软件排行榜
  • 音乐类网站建设选题背景太原百度快速优化