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

Already on GitHub? Sign in to your account

Allow timeago to be used with jquery-livequery #30

Open
wants to merge 1 commit into
from

Conversation

Projects
None yet

livequery extends selectors to find elements "even after the page has been loaded and the DOM updated".

This patch extends timeago to be used in conjunction with livequery.

The changes are pretty simple but simply not very pretty. I'd be happy if this idea would go into timeago even in a different form.

Allow timeago to be used with jquery-livequery:
$('attr.timeago').livequery(function() {
    $(this).timeago();
});

- Save intervalId to only issue setInterval once
- Add selected elements to a global array which we query

rmm5t commented on 038b8dc Jan 25, 2011

It seems, if you wanted to use timeago with livequery or similar (making timeago idempotent), you'd want to clear and reset the interval timer so that new elements are immediately updated with a fuzzy timestamp instead of waiting until the next refresh.

Line 113 takes care of updating elements right away. Through the nature of livequery though the whole refresh is done for each element matched by the selector. Adding content with 100 matches will trigger 100 refreshes in less than a second. This could use some cleaning-up.

Oh, absolutely. I missed that. Thanks. Did you have a use-case for the clear argument passed to the timeago function? I'd rather not change the signature just yet, as I'm planning on adding a full settings argument -- possibly a direct pull from Issue #24.

I'll run this through some tests and try to get it in soon.

I actually missed the clear argument. It was a part from a previously envisioned solution. So no, it is not needed. Tell me if you need me to update the commit.

Would like to see this pull request make it in too!

kaareal commented May 24, 2011

Yep me too

The included code works great, but has a performance problem. The elements array does not remove elements that are no longer visible on the page. I added the following code to remove elements that aren't visible:

  $.fn.timeago = function() {
    var self = this;
    self.each(refresh);

    // Only keep the elements that aren't hidden
    var visibleElements = [];
    $.each(elements, function(i, e) {
      if (!$(e).is(':hidden')) {
        visibleElements.push(e);
      }
    });
    elements = visibleElements;
    ...

I agree. Timeago being "accurate" to the minute, it would be make sense to have dynamically added elements updated too. It would defeat timeago's purpose if this feature or some similar solution is not implemented.

diegorv commented Mar 27, 2012

+1 :/

shichan commented Mar 30, 2012

+1 on merdjemak's comment. Hope this pull request makes it too

philfreo commented Jul 7, 2012

@cburgmer you should remove the clear argument.

Hope this pull request gets accepted... it's a major improvement even if you don't use jquery-livequery

Me too.

nasht00 commented Feb 11, 2014

Hello, this was opened a long time ago (no pun intended).
Is there a way today to attach timeago to live elements, without using external plugins?
Is there maybe a different github issue regarding this?

chalkup commented Feb 11, 2014

I use timeago with livequery on my site like this:

   $("time.timeago").livequery( ->
        $(this).timeago()
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment