31 lines
719 B
JavaScript
31 lines
719 B
JavaScript
export function throttle(func, delay) {
|
|
let timeoutId;
|
|
let lastExecTime = 0;
|
|
return function (...args) {
|
|
const currentTime = Date.now();
|
|
|
|
if (currentTime - lastExecTime > delay) {
|
|
func.apply(this, args);
|
|
lastExecTime = currentTime;
|
|
} else {
|
|
clearTimeout(timeoutId);
|
|
timeoutId = setTimeout(
|
|
() => {
|
|
func.apply(this, args);
|
|
lastExecTime = Date.now();
|
|
},
|
|
delay - (currentTime - lastExecTime),
|
|
);
|
|
}
|
|
};
|
|
}
|
|
|
|
export function createStyledElement(tag, styles = {}, attributes = {}) {
|
|
const element = document.createElement(tag);
|
|
|
|
Object.assign(element.style, styles);
|
|
Object.assign(element, attributes);
|
|
|
|
return element;
|
|
}
|