Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with "on change" #45

Open
KrunchMuffin opened this issue Jul 8, 2015 · 2 comments
Open

Issue with "on change" #45

KrunchMuffin opened this issue Jul 8, 2015 · 2 comments

Comments

@KrunchMuffin
Copy link

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
Copy link
Author

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
Copy link

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));
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants