From e80e2e9c2b4a77eae19864af8d5b30bbf9a25fd5 Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 21 Aug 2016 21:21:13 +0200 Subject: [PATCH] add check for undefined variables in moment-countdown.js and add comments --- public/js/moment-countdown.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/public/js/moment-countdown.js b/public/js/moment-countdown.js index 999cfcf5..302d75a1 100644 --- a/public/js/moment-countdown.js +++ b/public/js/moment-countdown.js @@ -1,11 +1,18 @@ +/** + * Initialize all moment countdowns on the page. A moment countdown has the + * class "moment-countdown" and the attribute "data-timestamp" which defines the + * countdown's goal. + */ $(document).ready(function() { + if (typeof moment !== "undefined") { $.each($('.moment-countdown'), function(i, e) { - var span = $(e); - var text = span.html(); - var timestamp = moment(parseInt(span.attr("data-timestamp") * 1000)); + var span = $(e); + var text = span.html(); + var timestamp = moment(parseInt(span.attr("data-timestamp") * 1000)); + span.html(text.replace("%c", timestamp.fromNow())); + setInterval(function() { span.html(text.replace("%c", timestamp.fromNow())); - setInterval(function() { - span.html(text.replace("%c", timestamp.fromNow())); - }, 1000); + }, 1000); }); + } }); \ No newline at end of file