easytimer.js
Version:
Timer/Chronometer/Countdown compatible with AMD and NodeJS
8 lines (6 loc) • 6.56 kB
JavaScript
/**
* easytimer.js
* Generated: 2023-08-28
* Version: 4.6.0
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).easytimer={})}(this,(function(t){"use strict";function e(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function n(t){for(var n=1;n<arguments.length;n++){var r=null!=arguments[n]?arguments[n]:{};n%2?e(Object(r),!0).forEach((function(e){o(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):e(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(t)}function o(t,e,n){return(e=function(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function i(t,e,n){var r,o="";if((t="number"==typeof t?String(t):t).length>e)return t;for(r=0;r<e;r+=1)o+=String(n);return(o+t).slice(-o.length)}function s(){this.reset()}function u(){this.events={}}s.prototype.toString=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:["hours","minutes","seconds"],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:":",n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:2;t=t||["hours","minutes","seconds"],e=e||":",n=n||2;var r,o=[];for(r=0;r<t.length;r+=1)void 0!==this[t[r]]&&("secondTenths"===t[r]?o.push(this[t[r]]):o.push(i(this[t[r]],n,"0")));return o.join(e)},s.prototype.reset=function(){this.secondTenths=0,this.seconds=0,this.minutes=0,this.hours=0,this.days=0},u.prototype.on=function(t,e){var n=this;return Array.isArray(this.events[t])||(this.events[t]=[]),this.events[t].push(e),function(){return n.removeListener(t,e)}},u.prototype.removeListener=function(t,e){if(Array.isArray(this.events[t])){var n=this.events[t].indexOf(e);n>-1&&this.events[t].splice(n,1)}},u.prototype.removeAllListeners=function(t){t?Array.isArray(this.events[t])&&(this.events[t]=[]):this.events={}},u.prototype.emit=function(t){for(var e=this,n=arguments.length,r=new Array(n>1?n-1:0),o=1;o<n;o++)r[o-1]=arguments[o];Array.isArray(this.events[t])&&this.events[t].forEach((function(t){return t.apply(e,r)}))};var c="secondTenths",a="seconds",f="minutes",h="hours",l="days",d=[c,a,f,h,l],p={secondTenths:100,seconds:1e3,minutes:6e4,hours:36e5,days:864e5},v={secondTenths:10,seconds:60,minutes:60,hours:24};function y(t,e){return(t%e+e)%e}function m(){var t,e,o,i,m,b,g,w,O,j,T=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},A=new s,P=new s,S=new u,E=!1,D=!1,L={},x={detail:{timer:this}};function V(t,e){var n=P[e];return function(t,e){var n=v[t];P[t]=e,A[t]=t===l?Math.abs(e):y(e>=0?e:n-y(e,n),n)}(e,B(t,p[e])),P[e]!==n}function M(){U(),A.reset(),P.reset()}function U(){clearInterval(t),t=void 0,E=!1,D=!1}function k(n){X()?(O=I(),b=F(m.target)):R(n),function(){var n=p[e];if(N(C(Date.now())))return;t=setInterval(_,n),E=!0,D=!1}()}function I(){return C(Date.now())-P.secondTenths*p[c]*o}function _(){var t=C(Date.now());!function(t){t[c]&&Q("secondTenthsUpdated",x);t[a]&&Q("secondsUpdated",x);t[f]&&Q("minutesUpdated",x);t[h]&&Q("hoursUpdated",x);t[l]&&Q("daysUpdated",x)}(z()),i(x.detail.timer),N(t)&&(H(),Q("targetAchieved",x))}function z(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:C(Date.now()),e=o>0?t-O:O-t,n={};return n[c]=V(e,c),n[a]=function(t){return V(t,a)}(e),n[f]=function(t){return V(t,f)}(e),n[h]=function(t){return V(t,h)}(e),n[l]=function(t){return V(t,l)}(e),n}function C(t){return Math.floor(t/p[e])*p[e]}function N(t){return b instanceof Array&&t>=j}function R(t){var n;e=function(t){if(t="string"==typeof t?t:a,!function(t){return d.indexOf(t)>=0}(t))throw new Error("Error in precision parameter: ".concat(t," is not a valid value"));return t}((t=t||{}).precision),i="function"==typeof t.callback?t.callback:function(){},w=!0===t.countdown,o=!0===w?-1:1,"object"===r(t.startValues)?(n=t.startValues,g=q(n),A.secondTenths=g[0],A.seconds=g[1],A.minutes=g[2],A.hours=g[3],A.days=g[4],P=G(g,P)):g=null,O=I(),z(),"object"===r(t.target)?b=F(t.target):w?(t.target={seconds:0},b=F(t.target)):b=null,L={precision:e,callback:i,countdown:"object"===r(t)&&!0===t.countdown,target:b,startValues:g},m=t}function q(t){var e;if("object"===r(t))if(t instanceof Array){if(5!==t.length)throw new Error("Array size not valid");e=t}else{for(var n in t)if(d.indexOf(n)<0)throw new Error("Error in startValues or target parameter: ".concat(n," is not a valid input value"));e=[t.secondTenths||0,t.seconds||0,t.minutes||0,t.hours||0,t.days||0]}e=e.map((function(t){return parseInt(t,10)}));var o=e[0],i=e[1]+B(o,10),s=e[2]+B(i,60),u=e[3]+B(s,60),c=e[4]+B(u,24);return e[0]=o%10,e[1]=i%60,e[2]=s%60,e[3]=u%24,e[4]=c,e}function B(t,e){var n=t/e;return n<0?Math.ceil(n):Math.floor(n)}function F(t){if(t){var e=G(b=q(t));return j=O+e.secondTenths*p[c]*o,b}}function G(t,e){var n=e||{};return n.days=t[4],n.hours=24*n.days+t[3],n.minutes=60*n.hours+t[2],n.seconds=60*n.minutes+t[1],n.secondTenths=10*n.seconds+t[[0]],n}function H(){M(),Q("stopped",x)}function J(t,e){S.on(t,e)}function K(t,e){S.removeListener(t,e)}function Q(t,e){S.emit(t,e)}function W(){return E}function X(){return D}R(T),void 0!==this&&(this.start=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t=n(n({},T),t),W()||(k(t),Q("started",x))},this.pause=function(){U(),D=!0,Q("paused",x)},this.stop=H,this.reset=function(){M(),k(m),Q("reset",x)},this.isRunning=W,this.isPaused=X,this.getTimeValues=function(){return A},this.getTotalTimeValues=function(){return P},this.getConfig=function(){return L},this.addEventListener=J,this.on=J,this.removeEventListener=K,this.removeAllEventListeners=function(t){S.removeAllListeners(t)},this.off=K)}t.Timer=m,t.default=m,Object.defineProperty(t,"__esModule",{value:!0})}));