diff --git a/jquery.timeago.js b/jquery.timeago.js index 2e8d29f5..83510bd9 100644 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -26,7 +26,17 @@ } }; var $t = $.timeago; - + + // destroyed event handler comes from: + // http://stackoverflow.com/questions/2200494/jquery-trigger-event-when-an-element-is-removed-from-the-dom/10172676#10172676 + jQuery.event.special.timeagodestroyed = { + remove: function(o) { + if (o.handler) { + o.handler($(this)) + } + } + }; + $.extend($.timeago, { settings: { refreshMillis: 60000, @@ -107,16 +117,32 @@ } }); + $.fn.timeago = function() { var self = this; self.each(refresh); var $s = $t.settings; if ($s.refreshMillis > 0) { - setInterval(function() { self.each(refresh); }, $s.refreshMillis); + var data = self.data('timeago') + data.intervalId = setInterval(function() { self.each(refresh); }, $s.refreshMillis); + self.data('timeago', data); + self.bind('timeagodestroyed', function(element){ + element.untimeago(); + }); } return self; }; + + + $.fn.untimeago = function() { + var self = this; + var timeagoData = self.data('timeago'); + if((typeof(timeagoData)!='undefined')&&(typeof(timeagoData.intervalId)!='undefined')){ + clearInterval(timeagoData.intervalId); + self.removeData('timeago'); + } + }; function refresh() { var data = prepareData(this); diff --git a/test/index.html b/test/index.html index bd0fcae8..79b360d2 100644 --- a/test/index.html +++ b/test/index.html @@ -194,6 +194,14 @@

Settings

  • [90 sec]
  • [120 sec]
  • + +

    untimeago

    + @@ -225,6 +233,18 @@

    Settings

    $("time.timeago").timeago(); var tooltip = $("#testTooltip").data("timeago"); + var untimeago1_initial = $("#testUntimeago1").data("timeago") + $("#testUntimeago1").untimeago(); + var untimeago1_final = $("#testUntimeago1").data("timeago") + var untimeago2_initial = $("#testUntimeago2").data("timeago") + $("#testUntimeago2").remove(); + var untimeago2_final = $("#testUntimeago2").data("timeago") + var untimeago3_initial = $("#testUntimeago3").data("timeago"); + $("#testUntimeago3").remove(); + var untimeago3_final = $("#testUntimeago3").data("timeago"); + var untimeago4_initial = $("#testUntimeago4").data("timeago"); + $("#testUntimeago4").untimeago(); + var untimeago4_final = $("#testUntimeago4").data("timeago"); $("abbr.todate").each(function () { var date = $.timeago.parse(this.title); @@ -563,6 +583,30 @@

    Settings

    test("wordSeparator", function () { ok($("#testNoSpaces1").html().match(/^2minutesago$/), "Settings correctly applied"); }); + + module("Untimeago"); + + test("manualRemove", function () { + ok(typeof(untimeago1_initial.intervalId)!='undefined', "Timeago interval handler defined"); + ok(untimeago1_initial.intervalId > -1, "Timeago interval handler valid"); + ok(typeof(untimeago1_final)=='undefined', "Timeago interval handler removed after untimeago"); + }); + + test("Remove", function () { + ok(typeof(untimeago2_initial.intervalId)!='undefined', "Timeago interval handler defined"); + ok(untimeago2_initial.intervalId > -1, "Timeago interval handler valid"); + ok(typeof(untimeago2_final)=='undefined', "Timeago interval handler removed after untimeago"); + }); + + test("RemoveOnNonTimeago", function () { + ok(typeof(untimeago3_initial)=='undefined', "testUntimeago3 is not a timeago element"); + ok(typeof(untimeago3_final)=='undefined', "calling remove() on non-timeago element doesn't cause crash"); + }); + + test("UntimeagoOnNonTimeago", function () { + ok(typeof(untimeago4_initial)=='undefined', "testUntimeago4 is not a timeago element"); + ok(typeof(untimeago4_final)=='undefined', "calling untimeago() on non-timeago element doesn't cause crash"); + }); })(jQuery); //]]>