Time display does not advance

I have the current date and time being displayed on one of the pages in my site. However, I’ve noticed that the time displayed does not advance (tick over) automatically as I expected. The display only shows the time when the page was first accessed. I can’t find what to add to my code to make the time move forward automatically.

My code:

$w.onReady(function () {
 
 const today = new Date();
 const options = {
        day: "numeric",
        month: "short",
        year: "numeric"
    };

    $w("#currentDate").text = today.toLocaleDateString("en-GB", options);
    $w("#currentTime").text = today.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })
 
});

Any help or pointing to a resource would be appreciated.

  1. Write this code outside the on ready function.
    2 .Write all this code inside one function like function automaticTime ();
    Then in on ready function write below code;
    automaticTime ();

And add at the end of the function ;
setTimeout(automaticTime,1000);

Hope it helps:)
Tell me if you are facing any issues still

Final code will be


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

 function automaticTime(){
 const today = new Date();
 const options = {
        day: "numeric",
        month: "short",
        year: "numeric"
    };

    $w("#currentDate").text = today.toLocaleDateString("en-GB", options);
    $w("#currentTime").text = today.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
    setTimeout(automaticTime,1000);
 
 }

Explanation of code:
We calling same time setting function for infinite time with the interval of 1 second (1000milliSeconds);
That’s it:)

Thank you so much! I will try it out and let you know how it goes!

If you are using only minute then replace by this

setTimeout(automaticTime,60000);

Which means function will call for every one minute;


https://muthu35.wixsite.com/mysite/clock
You can check Above Link


// For full API documentation, including code examples, visit https://wix.to/94BuAAs

$w.onReady(function () {
 // TODO: write your page related code here...
    automaticTime();

});

function automaticTime(){
 const today = new Date();
 const options = {
        day: "numeric",
        month: "short",
        year: "numeric"
    };

    $w("#currentDate").text = today.toLocaleDateString("en-GB", options);
    $w("#currentTime").text = today.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" , second : "2-digit"});

    setTimeout(automaticTime,1000);
 
 }

Hi,
What happened?
It working?

Is this working ?

@dean41146
Is this Working Fine ?

@Er.Muthu K
Thank you for this! Exactly what I wanted. Thank you!