Skip to content

Instantly share code, notes, and snippets.

@sk-t3ch
Last active February 24, 2022 01:45
Show Gist options
  • Save sk-t3ch/478f674594c40f0b2416e71d025e6d1d to your computer and use it in GitHub Desktop.
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.
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;
}
);
@sk-t3ch
Copy link
Author

sk-t3ch commented Feb 23, 2022

Plotting the outputs show:

pandas.DataFrame([1645634113715,
1645634115966,
1645634119343,
1645634124408,
1645634132003,
1645634143448,
1645634160537,
1645634186169,
1645634224618,
1645634282450,
1645634368951,
1645634498704,
1645634679578,
1645634860487,
1645635040566,
1645635220573,
1645635400581]).diff().plot(style='.')

output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment