Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Ilya Human ilyahuman

🏠
Working from home
View GitHub Profile
View JS sync sleep function
// Function will wait until needed time but will block your eventloop
function sleep(time = 0) {
const wakeUpTime = Date.now() + time
while (Date.now() < wakeUpTime) {}
}
View JS sleep function (for async)
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
View TimersManager
interface Timer {
name: string;
delay: number;
interval: boolean;
cb: Function;
}
interface TimerForCalling {
name: string;
interval: boolean;
View setTimeout with dynamic timeout
let timerId = setTimeout(function tick() {
timerId = setTimeout(tick, 2000) // here you can pass any timeout and dynamically change next calls
}, 1000);
// Example
let call = 1;
let timerId = setTimeout(function tick() {
timerId = setTimeout(tick, 2000 * call);
call++;
View Object deep cloning
const makeDeepClone = (obj) => {
let newObject = {};
Object.keys(obj).map(key => {
if(typeof obj[key] === 'object'){
newObject[key] = makeDeepClone(obj[key]);
} else {
newObject[key] = obj[key];
}
});