Last active
February 24, 2022 01:45
-
-
Save sk-t3ch/478f674594c40f0b2416e71d025e6d1d to your computer and use it in GitHub Desktop.
Set a dynamic interval with JS. This example shows a growing interval for logs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dynamicSetInterval = (func, getInterval) => { | |
setTimeout(() => { | |
func(); | |
dynamicSetInterval(func, getInterval); | |
}, getInterval()); | |
}; | |
let currentInterval = 1000; // 1 second | |
const maxInterval = 1000 * 60 * 3; // 3 minutes | |
const growthFactor = 1.5; | |
dynamicSetInterval(() => console.log('hi', new Date().getTime()), | |
() => { | |
currentInterval = Math.min(currentInterval * growthFactor, maxInterval); | |
return currentInterval; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Plotting the outputs show: