理解javascript debouncing
所提供的代码片段概述了该函数的实现:函数debounce(func,wait,wait,立即){ VAR超时; 返回函数(){ var context = this,args = garmuments; var later = function(){ 超时= null; 如果(! }; var callnow =立即&&!timeout; ClearTimeOut(超时); 超时= settimeout(稍后等待); if(callnow)func.apply(上下文,args); }; }; 要了解其工作原理,让我们分析每个部分:
此optional flag确定该函数是否应在延迟期Elapses之前的第一个呼叫上立即在第一个呼叫上立即执行。 If immediate is set to true, the function will run on the initial call and then apply the delay for subsequent calls.'timeout' variable:
Used internally to store the reference to a pending timeout.function debounce(func, wait, immediate) { var timeout; return function() { var context = this, args = arguments; var later = function() { timeout = null; if (!immediate) func.apply(context, args); }; var callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); }; };
'later' function:
Scheduled to run after the delay period.它清除了超时,如果“立即”是错误的,则执行拒绝的函数。免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3