У меня есть текущая функция метки времени, которая возвращает дату и время, но она не перемещается (имеется в виду: секунды, минуты и т. д. просто показывают, когда страница загружена и не работает)
Чего я хочу: Я хочу, чтобы время двигалось, а не было статичным.
script
methods: {
currentDateTime() {
const current = new Date();
const date =
current.getFullYear() +
"-" +
(current.getMonth() + 1) +
"-" +
current.getDate();
const time =
current.getHours() +
":" +
current.getMinutes() +
":" +
current.getSeconds();
const dateTime = date + " " + time;
return dateTime;
},
}
HTML
{{ currentDateTime() }}
screenshot
Есть идеи?



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


currentDateTime() будет выполнен только один раз. Используйте setInterval() для выполнения каждую 1 секунду.
data: {
timestamp: ''
},
mounted: function () {
setInterval(() => { this.currentDateTime() }, 1000)
}
}),
methods: {
currentDateTime() {
const current = new Date();
const date =
current.getFullYear() +
"-" +
(current.getMonth() + 1) +
"-" +
current.getDate();
const time =
current.getHours() +
":" +
current.getMinutes() +
":" +
current.getSeconds();
const dateTime = date + " " + time;
this.timestamp = dateTime;
},
}
HTML
{{ timestamp }}
Не забудьте поставить
clearIntervalвbeforeDestroy.