mattbradley / livestampjs

A simple, unobtrusive jQuery plugin that provides auto-updating timeago text to your timestamped HTML elements using Moment.js.
http://mattbradley.github.com/livestampjs
MIT License
409 stars 70 forks source link

Issue with "on change" #45

Open KrunchMuffin opened 9 years ago

KrunchMuffin commented 9 years ago

Everything works as it should until I implement the listener.

$('#Timestamp').on('change.livestamp', function(event, from, to) {
            event.preventDefault(); // Stop the text from changing automatically
            console.log(from,to);
            if(to.indexOf("ago")){
                $(this).removeClass("label-success label-warning").addClass("label-danger").html(to);
            } else {
                $(this).html(to);
            }
        }).livestamp();

It goes from saying 3 hours ago to saying a few seconds ago on load. Like it's just using the current time instead of the data attribute time provided.

KrunchMuffin commented 9 years ago

So I did some step through debug. It seems it runs through once and does it's thing ok, then it runs through a 2nd time, assuming cause of the listener, and since data-livestamp no longer exists since it is removed on line 16, it defaults to the current time.

timpalander commented 8 years ago

I know it's a bit old, but if anyone has the same problem - here's how i solved this (note: i could not get it to work with unix timestamp):

HTML <span class="timestamp" data-custom-livestamp="2015-06-09 16:44:37 +0200"></span>

Javascript (after DOM ready)

$("*[data-custom-livestamp]").each(function(value){
    var timestamp = this.dataset.customLivestamp;
    delete this.dataset.customLivestamp;
    $(this).on('change.livestamp', function(event, from, to) {
        // custom init code
    }).livestamp(new Date(timestamp));
});