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

Only bind timeago plugin to an element once #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

stevenharman
Copy link

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.

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.
@stevenharman
Copy link
Author

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

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

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants