0

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;
    }
},
asdfghjklm
  • 643
  • 2
  • 7
  • 18
  • Nobody could even give me a hint? I'm completely lost on this.. most things I can figure out by doing some reading or researching but this seems .. perplexing. – asdfghjklm Jun 24 '18 at 01:58

1 Answers1

0

Figured it out. I just created an object outside of everything called timers and pushed the timers into the object to be referenced later for destruction. Sorry for being so vague, I actually wrote this code in addition to my own to display an idea of what I did without revealing what I'm doing / any of the more business-like logic behind it.

I hope that this helps someone, but I somehow think this circumstance was sort of niche.

If anybody has a better answer which would increase performance, I would love to hear it. This is my hack for now.

let timers = {};

Actions: async (_, number, { user }) => {
  try {
    await requireAuth(user);
    let key = user._id;

    /// if >= 0, we start timer, if it's less than 0 we end the timer.
    if (number >= 0) {
      if (timer[key]) {
        return {
          /* graphql resolver return here ( since the timer already exists, we don't start it again! )*/
        };
      } else {
        startTimer(key);
      }
    } else {
      clearTimer(key);
    }

    function clearTimer(key) {
      clearInterval(timers[key].timers);
      delete timers[key];
      return {
        /* graphql resolver return here */
      };
    }

    function startTimer(key) {
      let user_timer = setInterval(doStuff, 6000);
      timers[key] = { ...timers[key], user_timer };
      return {
        /* ... graphql resolver return here */
      };
    }

    function doStuff() {
      if (/*numberOfTimersDecreenting */) {
        /* DO STUFF */
      } else { // if the user is out of timed actions
        clearTimer(key);
      }
    }
  } catch (e) {
    throw e;
  }
};
asdfghjklm
  • 643
  • 2
  • 7
  • 18