define(["jquery"], function($) { "use strict"; var config = { counterDigitSmallClass: 'pf-digit-counter-small', counterDigitLargeClass: 'pf-digit-counter-large' }; var updateDateDiff = function(element, tempDate){ var date1 = new Date(); var date2 = tempDate; //Customise date2 for your required future time var diff = (date1 - date2)/1000; diff = Math.abs(Math.floor(diff)); var days = Math.floor(diff/(24*60*60)); var leftSec = diff - days * 24*60*60; var hrs = Math.floor(leftSec/(60*60)); leftSec = leftSec - hrs * 60*60; var min = Math.floor(leftSec/(60)); leftSec = leftSec - min * 60; var value = []; if( days > 0 || value.length > 0 ){ value.push('' + days + 'd' + ''); } if( hrs > 0 || value.length > 0 ){ value.push('' + hrs + 'h' + ''); } if( min > 0 || value.length > 0 ){ value.push('' + min + 'm' + ''); } if( leftSec >= 0 || value.length > 0 ){ value.push('' + leftSec + 's' + ''); } element.html(value.join(' ')); }; /** * init a live counter based on a unix timestamp * @returns {*} */ $.fn.initTimestampCounter = function(){ return this.each(function(){ var element = $(this); var timestamp = parseInt( element.text() ); // do not init twice if(timestamp > 0){ // mark as init element.attr('data-counter', 'init'); var date = new Date( timestamp * 1000); updateDateDiff(element, date); var refreshIntervalId = window.setInterval(function(){ // update element with current time if( !element.hasClass('stopCounter')){ updateDateDiff(element, date); }else{ clearInterval( element.data('interval') ); } }, 100); element.data('interval', refreshIntervalId); } }); }; });