How to use the if statement in SetInterval

Dear Community,

Firstly allow me to apologize for any rookie mistakes. I have only recently started WiX and even though I am truly impressed with Velo I have yet to learn how to code properly. In the meanwhile, I’m trying to make the best out of all your examples and helpful community discussion hence why I turn to you.

For my to be created page I wanted to use a multi-state box to show and change between 2 different slogans on the homepage. Here is my page: https://remy664.wixsite.com/mysite

Here is my code as of now. I understand that there is a parsing error and it doesn’t expect the ‘.then’ statement. I’ve tried rewriting the code a couple of times but I just can’t seem to solve the problem.

$w.onReady(function () {
            {
                setInterval(async function () {
 if ("#statebox10") currentState = ("1")

                        $w("#statebox10").changeState("2");
                        .then((newState) =>
                            console.log(`Done moving to ${newState.id}`),

 else

                                $w("#statebox10").changeState("1");
                            .then((newState) => {
                                console.log(`Done moving to ${newState.id}`);

                            }); 
                        }, 5000);
                }
            });

Kindly let me know if you have any suggestions,
Thanks in advance!

Remy

$w.onReady(function () {
  {setInterval(async function () {
      if ("#statebox10").currentState = ("1") {
          $w("#statebox10").changeState("2");
          .then((newState) => {
              console.log(`Done moving to ${newState.id}`),
          })
      }
      else {$w("#statebox10").changeState("1");
          .then((newState) => {
              console.log(`Done moving to ${newState.id}`);
           }); 
      }
   }, 5000);
});

But i still do not understand your code :grin:

$w.onReady(function () {
  {setInterval(async function () {
      if ($w("#statebox10").currentState === "1") {
          await $w("#statebox10").changeState("2");
      }
      else {
          await $w("#statebox10").changeState("1");   
      }
      console.log($w("#statebox10").currentState)
   }, 5000); 
});

Improved version…

$w.onReady(function () {
  {setInterval(async function () {
      let myMSB = $w("#statebox10")
      if (myMSB.currentState === "1")  {await myMSB.changeState("2");}
      else                             {await myMSB.changeState("1");}
      console.log(myMSB.currentState)
   }, 5000);
});

Some examples for the “interval-function” … you will find here…
https://russian-dima.wixsite.com/meinewebsite

For example…
https://russian-dima.wixsite.com/meinewebsite/test-1

You’re a hero Dima. I will have a look at your examples and give it another shot.

Thank you for you help!

@remy51720 :wink:
If this one do not work… (i did not test it)…

$w.onReady(function () {
  {setInterval(async function () {
      let myMSB = $w("#statebox10")
      if (myMSB.currentState === "1")  {await myMSB.changeState("2");}
      else                             {await myMSB.changeState("1");}
      console.log(myMSB.currentState)
   }, 5000);
});

You can also do it like this…

$w.onReady(function () {
   setInterval(()=> {
      let myMSB = $w("#statebox10")
      if (myMSB.currentState === "1") {
         myMSB.changeState("2"), console.log(myMSB.currentState);
      }
      else {myMSB.changeState("1"), console.log(myMSB.currentState);}
   },5000);
});

Found an SYNTAX-MISTAKE here…

--> { <-- not needed  setInterval(async function () {

or trying this method… (your first try)

$w.onReady(function () {
   setInterval(()=> {
      let myMSB = $w("#statebox10")
      if (myMSB.currentState === "1") {
         myMSB.changeState("2").then(()=>{console.log(myMSB.currentState);})
      }
      else {myMSB.changeState("1").then(()=>{console.log(myMSB.currentState);})
   },5000);
});

Alright, quickly following up on this. Thanks to the help of @russian-dima I’ve simplified the code and made it work :grin: I’ve kept it very minimal as the purpose of this code is very simple. For anyone interested here’s the code:

$w.onReady(function () {
    {
        setInterval(function () {
 let myMSB = $w("#statebox10").currentState.id
            console.log(myMSB)
 if ((myMSB) === "1") $w("#statebox10").changeState("2");

 else
                $w("#statebox10").changeState("1")
        }, 8000);
    }
});

Well done!