1

I have a ResetTime function implemented that I need to stop once a modal is being called. The ResetTime function is being used for a session timeout. Everytime there is user interaction the time gets reset.

ResetTime function

function ResetTime() {
    timer = SetLastResetTimeStamp((new Date()).getTime());
} 

function SetLastResetTimeStamp(timeStamp) {
    if (_localStorage) {
        _localStorage[_localStorageKey] = timeStamp;
    } else {
        _lastResetTimeStamp = timeStamp;
    }
}

User Interaction checks:

AttachEvent(document, 'click', ResetTime);
AttachEvent(document, 'mousemove', ResetTime);
AttachEvent(document, 'keypress', ResetTime);
AttachEvent(window, 'load', ResetTime);

Is there a way to stop the ResetTime function in another function?

3
  • 1
    if(modal is open) return; Commented Sep 11, 2018 at 18:23
  • just like in real life, you set a condition. in real life: if driving -> don't hold phone and so in code it's only logical to set up conditions as well. you need to use if statement, and then set the condition for which the function should be called in the first place, or if the condition cannot be set there then you should set it within the function itself, like @Phiter wrote above. Commented Sep 11, 2018 at 18:25
  • Maybe the solution will be to logging time when modal is open, and change the business logic somehow. Otherwise, a solution will be declaring a global variable (instead is worst, because polluting global namespace), then if(modalIsOpen){myGlobVar=true}. In SetLastResetTimeStamp check myGlobVar, if false do localstorage stuff, else return. In this way you don't need to detach-reattach the events. Is just an idea ... Commented Sep 11, 2018 at 19:43

1 Answer 1

1

The solution suggested by @Phiter would work, i.e. you can define ResetTime in a scope containing a flag modalIsOpen and then define it as follows:

function ResetTime() {
  if (modalIsOpen) {
    return;
  }

  timer = SetLastResetTimeStamp((new Date()).getTime());
}

However, I think it would be cleaner to detach the events when your modal is opened and reattach them when it's getting closed. This will stop the function from getting called in the first place. I.e. assuming you have two functions onModalOpen and onModalClose and a DetachEvent function which removes events from the respective EventTargets:

const onModalOpen = () => {
    DetachEvent(document, 'click', ResetTime);
    DetachEvent(document, 'mousemove', ResetTime);
    DetachEvent(document, 'keypress', ResetTime);
    DetachEvent(window, 'load', ResetTime);

    // ...
}

const onModalClose = () => {
    AttachEvent(document, 'click', ResetTime);
    AttachEvent(document, 'mousemove', ResetTime);
    AttachEvent(document, 'keypress', ResetTime);
    AttachEvent(window, 'load', ResetTime);

    // ...
}

NOTE: Unbinding and rebinding event handlers is not uncontroversial though (see this answer).

As a sidenote, you cannot stop functions in JavaScript (or in most programming languages). You can only stop them from being executed (e.g. by returning early, throwing errors or simply not calling them in the first place.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.