How to make code run once in masterPage.js?

Hello, i have some code that i’m running in my masterpage.js to check time and conditionnaly render some elements, plus another DB call to populate a navigation element in my footer.
It’s running in my onReady function. I’m affraid that this code is running on every page change. How would i make the code run on first load only?

Thanks!

Let the code just run, if it is the right page-ID ! ! !

If page-ID = “myHomepage” —> run the code
else DO NOT RUN THE CODE !

You can keep a value in the memory and run the code if the value is undefined.
Something like:

//masterPage.js
import {memory} from "wix-storage";
$w.onReady(() => {
if(!memory.getItem("alreadyRun")){
    //run your code here
    memory.setItem("alreadyRun", "true");
}
}) 

Good evening J.D. (nice to see you frequently again ).:wink:

Your code (on MASTER-PAGE) runs on every page right? That means, no matter which page was opened once, the memory will be filled with TRUE-VALUE.

Not sure, but perhaps a combination of my and your code, could be the right symbiosis!? Or is my way of thinking wrong?

@russian-dima but that’s the point, my code runs only if the value in the memory is not true (meaning the first loaded page). When the value is true, it does not run.
and that’s exactly what Nicolas wanted.

if(!memory.getItem("alreadyRun"))

Hi,
JD’s snippet helped for my conditionnal rendering issue. The DB call seems more tricky to implement in my scenario, its pretty static data so i just hard coded my array for now :slight_smile: