How to throttle scroll and resize events in JavaScript

How to throttle scroll and resize events in JavaScript

Throttling functions control execution frequency by enforcing delays between calls, reducing CPU load during rapid events like scroll or resize. Basic and advanced throttle implementations handle leading and trailing calls, preserve context, and support cancellation for efficient event handling in JavaScript.