Drosselmuster-Typoskript [Duplikat]JavaScript

Javascript-Forum
Guest
 Drosselmuster-Typoskript [Duplikat]

Post by Guest »

Code: Select all

const throttle = (
callback: (...args: T) => void,
delay: number,
) => {
// this is not a global variable, just a local variable ??
let isWaiting = false;

return (...args: T) => {
if (isWaiting) {
return;
}

callback(...args);
// this just sets the local Variable to true ??
isWaiting = true;

setTimeout(() => {
isWaiting = false;
}, delay);
};
};
// Delay is 2000ms
window.addEventListener('resize', throttle(handleWindowResize, 2000));
Eines verstehe ich nicht.
Wenn die Throttle-Funktion ein zweites Mal aufgerufen wird, sagen wir nach 100 ms, warum ist die Variable isWaiting immer noch wahr? Es sollte nur eine lokale Variable sein und keinen Einfluss auf den zweiten Funktionsaufruf haben?? Das ist es, was ich nicht verstehe.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post