SOLVED: setTimeout() and setInterval() functions not behaving properly

This is crucial to the functionality of my site, but for some reason I can’t get it to work; hopefully one of you has the solution!

On my website, I have a sign-in page that re-directs the user to a dashboard page once they hit the sign-in button. The dashboard will contain sensitive information, so I want to user the setInterval() function to check every five minutes if the user would like to continue their session. After every five minutes, a lightbox will open and prompt the user to continue their current session, or to logout of their current session. If they choose neither options, and the lightbox remains open for more than one minute, they will automatically be re-directed to the homepage and logged off using the setTimeout() function.

Here is some of my code:

Dashboard page:

//opens the "Session Timeout" lightbox after 5 minutes
$w.onReady(function () {
setInterval(wixWindow.openLightbox("Session Timeout"), 300000);
});

Session timeout lightbox:

$w.onReady(function () {
setTimeout(logOutUser(), 60000);
});

//sends user to the homepage (homepage will automatically log out any visitors out if they're logged in)
function logOutUser(){
wixLocation.to('/');
}

From the above code you can see that I’m using:

  • setInterval() to open the Session Timeout lightbox every 5 minutes
  • setTimeout() to log the user out if the lightbox remains open more than one minute.

However, this is not how my site behaves at all. Instantly upon signing-in and being re-directed to the dashboard page, the Session Timeout box will open up (even though the timer was set to five minutes). Additionally, when the Session Timeout box is opened, I’m immediately sent back to the homepage when the code specified a one minute timer. The pages behave as if there’s no timers at all.

I’m hoping there’s just an error in my code somewhere and this isn’t just a glitch. If any of you bright minds out there have any ideas why this could be happening, please let me know!

Thank you in advance for any help.

Hi Kevin,

You are not using those function properly: checkout the d ocumentation

As you can read, setTimout and setInterval expect a function as a parameter. Your code executes the function instead of returning one, here is the correct call:

//opens the "Session Timeout" lightbox after 5 minutes
$w.onReady(function () {
   setInterval(() => wixWindow.openLightbox("Session 
   Timeout"), 300000);
   // and
   setTimeout(() => logOutUser(), 60000);
});

I hope this helps?

You’re not passing functions to them in the right way, the anonymous function looks like this:

 ( () => {
     // function body.
 })

Ahhh beautiful, works perfectly! I knew I was just missing something simple. You’re a lifesaver Quentin!