Only bind timeago plugin to an element once #26

Open
wants to merge 2 commits into
from

Projects

None yet

3 participants

@stevenharman

Currently, every time an element is passed into the $.fn.timeago() method that element is included in the setInterval, regardless of whether or not it has already been included in a previous interval.

We have found this problematic as we use a event to trigger binding of the timeago plugin, like so (within a Sammy.js app):
app.bind('widgetsRendered', function(e) {
$('.timestamp').timeago();
});

It would be ideal if only elements that were not already bound to timeago were included in the setInterval.

stevenharman added some commits Nov 18, 2010
@stevenharman stevenharman Issue 25: only bind timeago plugin once-per-element
This change will filter out any elements that have already been bound to
the timeago plugin so they are not included in multiple setInterval
closures.

NOTE: I also had to bump the version of jQuery used as 1.4.2 has a
bug where it wouldn't call the .each(callback). Perhaps this is due to
changes to the .filter method? Moving to 1.4.1 or 1.4.4
is also acceptable.
199d6c8
@stevenharman stevenharman Issue 20: don't bind nor setInterval for empty collections d488c95

I didn't realize the new Pull Request system auto-creates a new issue. I've closed the original Issue #25 and copied it's text here.

Sorry for the confusion

philfreo commented Jul 7, 2012

+1 for fixing this. Right now the naive way to use this plugin with dynamic content is to just call $('.timeago').timeago() after every time you add more content which is problematic because it causes the existing elements to get multiple setIntervals on them. Needs a fix like this.

#30/ is related but the plugin should work better without using a jquery plugin like livequery

porjo commented Feb 27, 2013

Wouldn't the solution be to use a global array that stores the IDs resulting from setInterval. Every call to setInterval does a clearInterval on the array of IDs (except for the last one) before appending the array with the new interval ID. This way, additional intervals can be set but will be prevented from accumulating

e.g.

if(id_array.length > 1) {
  for(var i=0; i < id_array.length-1; i++) {
    clearInterval(id_array[i]);
    id_array.splice(i,1);
  }
}
id_array.push(setInterval([...]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment