Skip to content

Commit 61980c6

Browse files
committed
Merge pull request #135 from enterprisey/master
Added 'dispose' action
2 parents 49aeb6b + 1a763d5 commit 61980c6

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

jquery.timeago.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
refresh_el();
129129
var $s = $t.settings;
130130
if ($s.refreshMillis > 0) {
131-
setInterval(refresh_el, $s.refreshMillis);
131+
this._timeagoInterval = setInterval(refresh_el, $s.refreshMillis);
132132
}
133133
},
134134
update: function(time){
@@ -138,6 +138,12 @@
138138
updateFromDOM: function(){
139139
$(this).data('timeago', { datetime: $t.parse( $t.isTime(this) ? $(this).attr("datetime") : $(this).attr("title") ) });
140140
refresh.apply(this);
141+
},
142+
dispose: function () {
143+
if (this._timeagoInterval) {
144+
window.clearInterval(this._timeagoInterval);
145+
this._timeagoInterval = null;
146+
}
141147
}
142148
};
143149

test/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ <h2>Settings</h2>
188188
<li><abbr id="testMillisSettings8" class="tomillis" title="90"></abbr> [90 sec]</li>
189189
<li><abbr id="testMillisSettings9" class="tomillis" title="120"></abbr> [120 sec]</li>
190190
</ul>
191+
192+
<h2>Disposal</h2>
193+
<p><abbr class="disposal disposed"></abbr></p>
194+
<p><abbr class="disposal notDisposed"></abbr></p>
191195
</div>
192196

193197
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
@@ -238,6 +242,8 @@ <h2>Settings</h2>
238242
$("abbr.tonumbers").each(toWords);
239243
unloadNumbers();
240244

245+
setupDisposal();
246+
241247
loadYoungOldYears();
242248
$("abbr.toyoungold").each(toWords);
243249

@@ -582,6 +588,28 @@ <h2>Settings</h2>
582588
ok($("#testNoSpaces1").html().match(/^2minutesago$/), "Settings correctly applied");
583589
ok($("#testNullSpaces1").html().match(/^2minutesago$/), "Settings correctly applied");
584590
});
591+
592+
module("Disposal");
593+
594+
asyncTest("disposal", function() {
595+
$(".disposal.disposed").timeago('dispose');
596+
var initialTime_disposedTimeago = $(".disposal.disposed").html();
597+
var initialTime_activeTimeago = $(".disposal.notDisposed").html();
598+
599+
expect(2);
600+
setTimeout(function() {
601+
var updatedTime_disposedTimeago = $(".disposal.disposed").html();
602+
var updatedTime_activeTimeago = $(".disposal.notDisposed").html();
603+
604+
ok(initialTime_disposedTimeago === updatedTime_disposedTimeago, "A disposed timeago didn't get updated");
605+
ok(initialTime_activeTimeago !== updatedTime_activeTimeago, "A non-disposed timeago continued to be updated");
606+
607+
// Dispose still-active timeago
608+
$(".disposal.notDisposed").timeago('dispose');
609+
resetRefreshMillis();
610+
start();
611+
}, 50);
612+
});
585613
})(jQuery);
586614
//]]>
587615
</script>

test/test_helpers.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ function unloadCutoffSetting() {
3030
jQuery.timeago.settings.cutoff = 0;
3131
}
3232

33+
function setupDisposal() {
34+
jQuery.timeago.settings.refreshMillis = 50;
35+
$('abbr.disposal').attr("title", iso8601(new Date())).timeago();
36+
}
37+
3338
function loadPigLatin() {
3439
jQuery.timeago.settings.strings = {
3540
suffixAgo: "ago-hay",
@@ -84,6 +89,10 @@ function loadRussian() {
8489
})();
8590
}
8691

92+
function resetRefreshMillis() {
93+
jQuery.timeago.settings.refreshMillis = 60000;
94+
}
95+
8796
function loadMillis() {
8897
var millisSubstitution = function(number, millis) { return millis + " milliseconds"; };
8998
jQuery.timeago.settings.strings = {

0 commit comments

Comments
 (0)