Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

setInterval will clear itself when element is removed from the DOM #98

Open
wants to merge 3 commits into from

2 participants

@scotthovestadt

My use-case is an activity feed with only 5 items. However, over the lifetime of the web application, thousands of items pass through the feed.

It was not ideal to have thousands of setInterval calls fire each minute.

@rupurt

Any word on this issue? Single page apps are becoming more common and timeago needs the ability to remove itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 15, 2012
  1. @scotthovestadt

    setInterval will clear itself when element removed

    scotthovestadt authored
    setInterval will clear itself when element is removed from the DOM.
  2. @scotthovestadt

    Updated to deal with multiple elements.

    scotthovestadt authored
    Credit to tangphillip.
  3. @scotthovestadt
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 1 deletion.
  1. +13 −1 jquery.timeago.js
View
14 jquery.timeago.js
@@ -113,7 +113,19 @@
var $s = $t.settings;
if ($s.refreshMillis > 0) {
- setInterval(function() { self.each(refresh); }, $s.refreshMillis);
+ var intervalId = setInterval(function() {
+ var destroyed = 0;
+ self.each(function() {
+ if($(this).parents(":last").is("html") == false) {
+ destroyed++;
+ } else {
+ refresh.call(this);
+ }
+ });
+ if (destroyed === self.length) {
+ clearInterval(intervalId);
+ }
+ }, $s.refreshMillis);
}
return self;
};
Something went wrong with that request. Please try again.