Help with JS code and Wix

I can’t believe I got this code to work. I made a basic counter white text in a black box and it increases every 15 seconds. But my Java is pretty rusty, and I need this for my website on Wix (ugh) and It only counts for the users’ sessions. It doesn’t keep track of what the number is through out the month and report that whenever someone visits the site. Just tell me is that going to be way more difficult as far as coding, or is it a local storage setting I have to change thru Wix? I’m trying to run a non-profit, and I just don’t have the funds for a programmer right now. (I took out a couple of operators and and what not because it’s trying to run some of my code in the reply lol)

html lang="en"
head
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Number Box</title>
    <style>
        .box {
            background-color: black;
            color: white;
            font-size: 48pt;
            padding: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="box" id="number-box">1</div>
    <script>
        let numberBox = document.getElementById("number-box");
        let counter = parseInt(localStorage.getItem("counter")) || 1;

        function updateNumber() {
            let today = new Date();
            let daysInMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0).getDate();

            if (today.getDate() === 1) {
                counter = 1;
            } else if (today.getDate() === daysInMonth) {
                clearInterval(intervalId);
            } else {
                counter++;
            }

            numberBox.innerHTML = counter;
            localStorage.setItem("counter", counter);
        }

        let intervalId = setInterval(updateNumber, 15000);
    </script>
</body>
</html> 

So a few things…

First, just so you don’t confuse yourself or anyone else in the future, you are using JavaScript, not Java. They are two very different things.

Secondly, localstorage is data that is kept on the user’s browser and as such can be modified or even erased by the user. If you are wanting to keep something accurate, you might want to look into solutions where you keep the data on the server instead for the user’s browser.

Lastly, localstorage is controlled by the browser as mentioned previously, not by WiX so your solution here should work fine on WiX as long as it lets you run JavaScript (I don’t see why it wouldn’t).

Hopefully that answers your question. :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.