Hola chicos, estos son mis códigos tratando de hacer que mi reloj funcione en Bootstrap Vue. Quiero que la hora se muestre en tiempo real con los segundos en movimiento. Las fechas funcionan y el tiempo funciona, pero el tiempo es estacionario. ¿Qué estoy haciendo mal? Novato..#gracias
<template> <div> <div> {{timestring}} </div> <div> {{timeclock}} </div> </div> </template> <script> data ()=> ({ timeString: '', timeclock: '', stopClock: false}), mounted () { this.nowTime(); this.nowclock(); }, methods: { nowTime () { this.timeString = new Date(Date.now()).toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); if (!this.stopClock) { setTimeout(this.nowTime, 285); } }, nowclock () { this.timeclock = +' ' + new Date(Date.now()).toLocaleTimeString('en-US'); if (!this.stopClock) { setTimeout(this.nowTime, 285); } } }, beforeDestroy () { this.stopClock = true; } };
Simplemente puede usar setTimeout
con 1000ms
intervalo de tiempo de 1000 ms con la función de devolución de llamada this.nowclock
nowclock() { this.timeclock = +" " + new Date(Date.now()).toLocaleTimeString("en-US"); if (!this.stopClock) setTimeout(this.nowclock, 1000); },