Pregunta

Tengo un valor 011-04-29T14:55:33.000Z este valor se inserta en una plantilla jQuery.Utilicé timeago para convertir la fecha en tiempo transcurrido, pero después de escribirla en la plantilla no tiene forma de actualizarse comopasa más tiempo.

¿Cómo implementaría algo que se actualice automáticamente?

¿Fue útil?

Solución

Suponga que comienza con esto (de la timeago página de inicio):

<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>

Ahora, el complemento timeago cambiará el título a medida que reescribe las cosas.Todo lo que necesita hacer es realizar un seguimiento de la marca de tiempo en otro lugar, volver a colocarla en el atributo title y volver a ejecutar el complemento.Algo como esto:

<abbr
    class="timeago"
    title="2008-07-17T09:24:17Z"
    data-ts="2008-07-17T09:24:17Z"
>July 17, 2008</abbr>

Se convertirá en esto:

<abbr
    class="timeago"
    title="July 17, 2008"
    data-ts="2008-07-17T09:24:17Z"
>2 years ago</abbr>

Y cuando desee actualizarlo, simplemente vuelva a colocar data-ts en title y vuelva a ejecutar el complemento:

$('.timeago').each(function() {
    var $this = $(this);
    $this.attr('title', $this.data('ts'));
}).timeago();

Si está utilizando un jQuery más antiguo, es posible que deba utilizar $this.attr('data-ts') en lugar de $this.data('ts').

Otros consejos

Intenté lo anterior sin suerte y encontré esto.podría ser útil.

https://mattbradley.github.io/livestampjs/

Aquí es suficiente <span data-livestamp="your time goes here..."></span>.

No olvide agregar jquery.js y moment.js antes de livestamp.js

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top