Skip to content

Instantly share code, notes, and snippets.

View HelgaZhizhka's full-sized avatar
👁️‍🗨️

Olga Zhyzhka HelgaZhizhka

👁️‍🗨️
View GitHub Profile
// Creates a throttled function that only invokes "originalFn" at most once per every "delayMs" milliseconds
function throttle(originalFn, delayMs) {
let timeout; // timeout to keep track of the executions
return (...args) => {
if (timeout) { // if timeout is set, this is NOT the first execution, so ignore
return;
}
// this is the first execution which we need to delay by "delayMs" milliseconds
timeout = setTimeout(() => {
function debounce(originalFn, timeoutMs) {
let timeout;
return (...args) => {
clearTimeout(timeout); // clear timeout every time the function is called
timeout = setTimeout(() => originalFn(...args), timeoutMs); // call the original function once "timeoutMs" ms after the last call have elapsed
};
}
@FranklinGuan
FranklinGuan / Codewars-4kyu-Human_readable_duration_format.js
Last active April 3, 2024 12:30
Human readable duration format 4kyu-Codewars
//Introduction
// Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.
// The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.
// It is much easier to understand with an example:
// formatDuration(62) // returns "1 minute and 2 seconds"
// formatDuration(3662) // returns "1 hour, 1 minute and 2 seconds"
// For the purpose of this Kata, a year is 365 days and a day is 24 hours.
// Note that spaces are important.
@argodeep
argodeep / polyfill.js
Last active May 19, 2025 13:08
Bind, Call, Apply Polyfill using function prototype
let car1 = {
color: 'Red',
company: 'Ferrari',
};
let car2 = {
color: 'Blue',
company: 'BMW',
};