i'd like to design a function that is not triggered by a button click, not onmount, not on unmount but on given set 'times' like below:
function () {
if this time comes: do this
also if this comes: do this
and if this time comes: do this
any time that is not one of the times above: do nothing
}
i currently, the options i know i have are using the useEffect hook, or setInterval, but am not sure if they can work, i dont know how to do it either. kindly suggest on how i can achieve this
CodePudding user response:
setInterval is great for executing a function at regular intervals. for instance:
function sayHelloWorld() {
console.log('Hello, World!');
}
// interval in milliseconds --> 1000ms = 1 sec...
const sayOverAndOver = setInterval(sayHelloWorld, 1000);
After 1 second and each second thereafter, the console will log 'Hello, World!'
Some good resources with great explainers on setInterval, etc. are W3Schools, GeeksForGeeks, and Mozilla MDN. I tried to link to them here, but the text editor on here is buggy.
Cheers!