Help with syntax

Hey everyone, can someone help me work out the correct syntax here? What I want is for the preloader strip only to be visible when the user visits the homepage for the first time that session, otherwise I want it to be hidden. I’m relatively new to code, and I keep getting a syntax error for the ‘else’ statement. Help please!

Here’s my code:

import {session} from 'wix-storage';

$w.onReady(function () {
  wait5Seconds();
});

function wait5Seconds() {
    setTimeout(() => {
        if(!session.getItem("firstTimeVisit")) {
        $w('#preloader').hide("fade")
        }
    session.setItem("firstTimeVisit", "yes")
     7000})
        else (
          $w('#preloader').hide();
        )};

Thanks in advance!

A couple of issues:

You have the if statement inside of the setTimeout() function, but you have the else statement outside. That’s one error. They need to be together, like this:

if (condition) {
   // do stuff
}
else {
   // do other stuff
}

You also incorrectly used parentheses for the else instead of curly brackets. That’s another error.

Now for a question… What is it you want to do? It seems that no matter if the first time or not, you wait 5 seconds (btw - 7000 is 7 seconds), and then hide the preloader. The only difference is that on first time you hide by fading, and every other time you just hide. What is your intention here?

Without knowing what you want, here is your code with some corrections:

import { session } from 'wix-storage';

$w.onReady(function () {
    wait5Seconds();
});

function wait5Seconds() {
    setTimeout(() => {
        if (session.getItem("firstTimeVisit") !== 'yes') {
            $w('#preloader').hide("fade");
            session.setItem("firstTimeVisit", "yes")
        } else {
            $w('#preloader').hide();
        }
    }, 5000);
}

Thanks for your help, Yisrael!

That almost works properly, though I only want the preload strip to show up and then timeout if it’s the user’s first visit. It’s timed to a video, so I don’t need to to be exactly 5 seconds, that’s just the name I gave the function.

If it’s not the first visit to the homepage in the session, I want it to automatically be hidden.

I’ve revised the code to reflect the changes, but now it’s not showing at all, even when I open the window in an entirely different browser.

Would really appreciate some support on this, thank you!

Here’s the updated code:

function wait5Seconds() {
        if (session.getItem("firstTimeVisit") === 'yes') {
            session.setItem("firstTimeVisit", "yes")
                setTimeout(() => {
                    $w('#preloader').hide("fade");
                    }, 8000);
        } else {
            $w('#preloader').hide();
        }
}

@beckett Sorry, that was a typo on my part (corrected my previous comment). The if statement should check “if not equal”, like this:

if (session.getItem("firstTimeVisit") !== 'yes') {