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.
muthu
August 18, 2020, 2:34am
2
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
muthu
August 18, 2020, 2:39am
3
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);
}
muthu
August 18, 2020, 2:54am
4
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!
muthu
August 18, 2020, 3:06am
6
If you are using only minute then replace by this
setTimeout(automaticTime,60000);
Which means function will call for every one minute;
muthu
August 18, 2020, 4:18am
8
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);
}
muthu
August 19, 2020, 1:42am
9
Hi,
What happened?
It working?
muthu
August 24, 2020, 12:42pm
11
@dean41146
Is this Working Fine ?
@Er.Muthu K
Thank you for this! Exactly what I wanted. Thank you!