#Can't make the time update every second

1 messages · Page 1 of 1 (latest)

unkempt coral
#

There is a script to get the current time and it should be updated every second. It does not work.
Can you tell me how to optimize it to make it work and display the result on the page so it shows the time online.

`export default {
wr: async () => {
const date = new Date();

const options = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: true,
timeZone: 'America/Nassau',
};

// US English uses month-day-year order
let a = new Intl.DateTimeFormat('en-US', options).format(date);
// "1/12/2023, 09:13:57"
return setInterval(()=> {a}, 1000);
}
}`

odd shore
#

In the setinterval do { return a}

unkempt coral
#

Like this?
return setInterval(()=> {return a}, 1000);
doesn't work
gives an error

meager steppe
#

Hi there!
The code block is not working as you might expect because you're declaring the date variable outside of your setInterval, so the function is called with the same value each time.
I suggest you move the date declaration and the let a = new Intl.DateTimeFormat('en-US', options).format(date) into the setInterval call.

Your setInterval call would look something like this:

setInterval(() => {
  const date = new Date();
  let a = new Intl.DateTimeFormat("en-US", options).format(date);
  return a
}, 1000);
unkempt coral
hallow solar
#

Hey there!
Could try moving the options declaration into the setInterval declaration as well? Also, don't forget to move the date instantiation as well.

unkempt coral
# hallow solar Hey there! Could try moving the options declaration into the setInterval declara...

I did this. No errors, but the function returns undefined

`export default {
wr: async () => {

// US English uses month-day-year order
// "1/12/2023, 09:13:57"
setInterval(() => {
const date = new Date();
const options = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: true,
timeZone: 'America/Nassau',
};
let a = new Intl.DateTimeFormat("en-US", options).format(date);
return a;
}, 1000);
}
}`

hallow solar
#

That’s weird. I’ll try to run this on my end and see what I get

unkempt coral
nocturne granite
#

Hi, any workaround for this? I try to create realtime clock for a dashboard. The app will show the date and time that must be updated every 1 second.

unkempt coral
# nocturne granite Hi, any workaround for this? I try to create realtime clock for a dashboard. The...

Hi.
Yes, I was able to create a real time clock.
The way it works for me is this.
I split the script.
First I added a script which outputs the time, but it is not loaded automatically and is called by another script via SetInterval which runs when the page loads.
` export default {
wr: async () => {
const date = new Date();
const options = {
//year: 'numeric',
//month: 'numeric',
//day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: true,
timeZone: 'America/Nassau',
};
// US English uses month-day-year order
// "1/12/2023, 09:13:57"
return new Intl.DateTimeFormat("en-US", options).format(date);
}
}

//Run on page load

export default {
async timeTrigger () {
await setInterval(() => { time_clock3.wr() }, 1000, "time");

// use async-await or promises
// await storeValue('varName', 'hello world')
}`