From 63ab5d0325ec7566c9c222449efe95ac65940f1a Mon Sep 17 00:00:00 2001 From: scotthovestadt Date: Thu, 15 Nov 2012 11:04:16 -0800 Subject: [PATCH 1/3] setInterval will clear itself when element removed setInterval will clear itself when element is removed from the DOM. --- jquery.timeago.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/jquery.timeago.js b/jquery.timeago.js index 2e8d29f5..879ae478 100644 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -113,7 +113,14 @@ var $s = $t.settings; if ($s.refreshMillis > 0) { - setInterval(function() { self.each(refresh); }, $s.refreshMillis); + var intervalID = setInterval(function() { + // Do not continue refresh if the element is no longer attached to the DOM. + if(self.parents(':last').is('html') == false) { + clearInterval(intervalID); + } else { + self.each(refresh); + } + }, $s.refreshMillis); } return self; }; From 61d3bc32a676cfc5cbd1548e3f2341a1d92ea5fc Mon Sep 17 00:00:00 2001 From: scotthovestadt Date: Thu, 15 Nov 2012 11:14:08 -0800 Subject: [PATCH 2/3] Updated to deal with multiple elements. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Credit to tangphillip. --- jquery.timeago.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/jquery.timeago.js b/jquery.timeago.js index 879ae478..bcc6c07f 100644 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -113,12 +113,17 @@ var $s = $t.settings; if ($s.refreshMillis > 0) { - var intervalID = setInterval(function() { - // Do not continue refresh if the element is no longer attached to the DOM. - if(self.parents(':last').is('html') == false) { - clearInterval(intervalID); - } else { - self.each(refresh); + var intervalId = setInterval(function() { + var destroyed = 0; + self.each(function() { + if($(this).parents(":last").is("html") == false) { + destroyed++; + } else { + Function.call(this, refresh); + } + }); + if (destroyed === self.length) { + clearInterval(intervalId); } }, $s.refreshMillis); } From 439dfd0fa49a3af02e3799353d3ed02f8c7f2828 Mon Sep 17 00:00:00 2001 From: scotthovestadt Date: Thu, 15 Nov 2012 12:05:04 -0800 Subject: [PATCH 3/3] Changed Function.call to refresh.call --- jquery.timeago.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.timeago.js b/jquery.timeago.js index bcc6c07f..b8b93608 100644 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -119,7 +119,7 @@ if($(this).parents(":last").is("html") == false) { destroyed++; } else { - Function.call(this, refresh); + refresh.call(this); } }); if (destroyed === self.length) {