理解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