I'm making an app which requires that I give each user a 6 second interval timer that will complete actions every 6 seconds. I already have it working that the timers can be unique based on the function but the main issue is that I cannot dynamically cancel them if I recall the function with a different parameter. I can only cancel timers by creating a method where at a certain count down it will clearInterval(action);
I basically set up the function so that I could pass two types of numbers. Negative or positive. Here is an example:
To be clear I cannot take the "actions" variable which the timer is assigned to and assign it outside of the function as it will only allow the server to run one-timer instead of the one-timer required for each user. I really just cannot figure this out!
graphql resolver
Actions: async (_, number, {user}) => {
try {
await requireAuth(user);
if (number > 0) {
let doActions = setInterval(doStuff, 6000);
} else {
clearInterval(doActions);
}
function doStuff() {
if (userRelatedNumber > 0) {
"...actions in here"
} else {
clearInterval(doActions); //// does not work with recalling function with -1
}
}
} catch (e) {
throw e;
}
},