Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added 'dispose' action #135

Merged
merged 2 commits into from

2 participants

Ryan McGeary Phil Freo
Deleted user
  • use with elements about to be removed from the DOM or that otherwise no longer require updating
  • only clears interval; leaves node as-is
  • usage: $('.timeago').timeago('dispose');
Ryan McGeary
Owner

Possible to add tests for this feature?

Dave added some commits
Dave Add 'dispose' action
- use with elements about to be removed from the DOM or that otherwise
no longer require updating
- only clears interval; leaves node as-is
- usage: $('.timeago').timeago('dispose');
3b89571
Dave Add tests for 'dispose' action 1a763d5
Deleted user

Line endings should be good now.

Ryan McGeary rmm5t merged commit 61980c6 into from
Ryan McGeary
Owner

@enterprisey Thanks! Sorry for the delay on this one. I'll try to get an official release out this week that incorporates this change too.

Phil Freo

@rmm5t along the same lines as dispose (not having extra timers, etc. around), can you take a look at #30 which (even when not using jquery-live) or similar so that calling autosave() multiple times (when new portions of the dom are rendered/re-rendered) that multiple instances of autosave don't get instantiated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 30, 2013
  1. Add 'dispose' action

    Dave authored
    - use with elements about to be removed from the DOM or that otherwise
    no longer require updating
    - only clears interval; leaves node as-is
    - usage: $('.timeago').timeago('dispose');
  2. Add tests for 'dispose' action

    Dave authored
This page is out of date. Refresh to see the latest.
Showing with 44 additions and 1 deletion.
  1. +7 −1 jquery.timeago.js
  2. +28 −0 test/index.html
  3. +9 −0 test/test_helpers.js
8 jquery.timeago.js
View
@@ -128,7 +128,7 @@
refresh_el();
var $s = $t.settings;
if ($s.refreshMillis > 0) {
- setInterval(refresh_el, $s.refreshMillis);
+ this._timeagoInterval = setInterval(refresh_el, $s.refreshMillis);
}
},
update: function(time){
@@ -138,6 +138,12 @@
updateFromDOM: function(){
$(this).data('timeago', { datetime: $t.parse( $t.isTime(this) ? $(this).attr("datetime") : $(this).attr("title") ) });
refresh.apply(this);
+ },
+ dispose: function () {
+ if (this._timeagoInterval) {
+ window.clearInterval(this._timeagoInterval);
+ this._timeagoInterval = null;
+ }
}
};
28 test/index.html
View
@@ -188,6 +188,10 @@
<li><abbr id="testMillisSettings8" class="tomillis" title="90"></abbr> [90 sec]</li>
<li><abbr id="testMillisSettings9" class="tomillis" title="120"></abbr> [120 sec]</li>
</ul>
+
+ <h2>Disposal</h2>
+ <p><abbr class="disposal disposed"></abbr></p>
+ <p><abbr class="disposal notDisposed"></abbr></p>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
@@ -238,6 +242,8 @@
$("abbr.tonumbers").each(toWords);
unloadNumbers();
+ setupDisposal();
+
loadYoungOldYears();
$("abbr.toyoungold").each(toWords);
@@ -582,6 +588,28 @@
ok($("#testNoSpaces1").html().match(/^2minutesago$/), "Settings correctly applied");
ok($("#testNullSpaces1").html().match(/^2minutesago$/), "Settings correctly applied");
});
+
+ module("Disposal");
+
+ asyncTest("disposal", function() {
+ $(".disposal.disposed").timeago('dispose');
+ var initialTime_disposedTimeago = $(".disposal.disposed").html();
+ var initialTime_activeTimeago = $(".disposal.notDisposed").html();
+
+ expect(2);
+ setTimeout(function() {
+ var updatedTime_disposedTimeago = $(".disposal.disposed").html();
+ var updatedTime_activeTimeago = $(".disposal.notDisposed").html();
+
+ ok(initialTime_disposedTimeago === updatedTime_disposedTimeago, "A disposed timeago didn't get updated");
+ ok(initialTime_activeTimeago !== updatedTime_activeTimeago, "A non-disposed timeago continued to be updated");
+
+ // Dispose still-active timeago
+ $(".disposal.notDisposed").timeago('dispose');
+ resetRefreshMillis();
+ start();
+ }, 50);
+ });
})(jQuery);
//]]>
</script>
9 test/test_helpers.js
View
@@ -30,6 +30,11 @@ function unloadCutoffSetting() {
jQuery.timeago.settings.cutoff = 0;
}
+function setupDisposal() {
+ jQuery.timeago.settings.refreshMillis = 50;
+ $('abbr.disposal').attr("title", iso8601(new Date())).timeago();
+}
+
function loadPigLatin() {
jQuery.timeago.settings.strings = {
suffixAgo: "ago-hay",
@@ -84,6 +89,10 @@ function loadRussian() {
})();
}
+function resetRefreshMillis() {
+ jQuery.timeago.settings.refreshMillis = 60000;
+}
+
function loadMillis() {
var millisSubstitution = function(number, millis) { return millis + " milliseconds"; };
jQuery.timeago.settings.strings = {
Something went wrong with that request. Please try again.