Skip to content

Instantly share code, notes, and snippets.

View ilyahuman's full-sized avatar
🏠
Working from home

Ilya Human ilyahuman

🏠
Working from home
View GitHub Profile
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];
}
});
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++;
interface Timer {
name: string;
delay: number;
interval: boolean;
cb: Function;
}
interface TimerForCalling {
name: string;
interval: boolean;
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
// Function will wait until needed time but will block your eventloop
function sleep(time = 0) {
const wakeUpTime = Date.now() + time
while (Date.now() < wakeUpTime) {}
}