Skip to content

Added 'dispose' action #135

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

Merged
merged 2 commits into from Aug 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion jquery.timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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;
}
}
};

Expand Down
28 changes: 28 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ <h2>Settings</h2>
<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>
Expand Down Expand Up @@ -238,6 +242,8 @@ <h2>Settings</h2>
$("abbr.tonumbers").each(toWords);
unloadNumbers();

setupDisposal();

loadYoungOldYears();
$("abbr.toyoungold").each(toWords);

Expand Down Expand Up @@ -582,6 +588,28 @@ <h2>Settings</h2>
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>
Expand Down
9 changes: 9 additions & 0 deletions test/test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 = {
Expand Down