-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeControll.min.js
1 lines (1 loc) · 3.12 KB
/
timeControll.min.js
1
function TimeControll(a,b,c,d,e,f,g){var h=null,i=86400,j={timeParam:a,decrement:b,startParam:c,just24:d,timeInThePage:e,getTime:f,debug:g};"object"==typeof a&&null!=a&&(j=a);var k=function(a){if(!a||a.length<8){var b=new Date,c=b.getHours(),d=b.getMinutes(),e=b.getSeconds();return c<10&&(c="0"+c),d<10&&(d="0"+d),e<10&&(e="0"+e),c+":"+d+":"+e}return a},l=function(a){var b=a.split(":"),c=b[0],d=b[1],e=b[2];return 60*(60*c+parseInt(d,10))+parseInt(e,10)},m=function(a){a<0&&(a*=-1);var b=Math.floor(a/60),c=Math.floor(b/60);c<10&&(c="0"+c),b%=60,b<10&&(b="0"+b);var d=a%60;return d<10&&(d="0"+d),c+":"+b+":"+d},n=function(){null!=h&&clearInterval(h),h=setInterval(function(){j.timeInThePage===!0&&document.hasFocus()===!0&&(j.secondsInPage++,j.timeInPage=m(j.secondsInPage));var b=l(a);b=j.decrement===!1?parseInt(b+1,10):parseInt(b-1,10),b<0&&(b=0,clearInterval(h)),j.just24&&b>=i&&j.decrement===!1&&(b=0);var c=m(b);a=c,j.debug===!0&&console.log(c),"function"==typeof j.getTime&&j.getTime(a,b,a.split(":"),j)},1e3)};return a=k(j.timeParam),delete j.timeParam,j.decrement="undefined"!=typeof j.decrement&&null!=j.decrement&&j.decrement,j.just24="undefined"!=typeof j.just24&&null!=j.just24&&j.just24,j.timeInThePage="undefined"!=typeof j.timeInThePage&&null!=j.timeInThePage&&j.timeInThePage,j.getTime="function"==typeof j.getTime?j.getTime:null,j.debug="undefined"!=typeof j.debug&&null!=j.debug&&j.debug,j.timeSet=a,j.timeActual=a,j.secondsInPage=0,j.timeInPage=0,j.setTime=function(b){return a=k(b),j.timeActual=a,j},j.addSeconds=function(b){return b=parseInt(b,10),a=m(l(a)+(isNaN(b)?0:b)),j.timeActual=a,j},j.addMinutes=function(b){return b=parseInt(b,10),a=m(l(a)+(isNaN(b)?0:60*parseInt(b,10))),j.timeActual=a,j},j.addHours=function(b){return b=parseInt(b,10),a=m(l(a)+(isNaN(b)?0:60*parseInt(b,10)*60)),j.timeActual=a,j},j.subSeconds=function(b){return b=parseInt(b,10),a=m(l(a)-(isNaN(b)?0:b)),j.timeActual=a,j},j.subMinutes=function(b){return b=parseInt(b,10),a=m(l(a)-(isNaN(b)?0:60*parseInt(b,10))),j.timeActual=a,j},j.subHours=function(b){return b=parseInt(b,10),a=m(l(a)-(isNaN(b)?0:60*parseInt(b,10)*60)),j.timeActual=a,j},j.start=function(){return n(),j},j.restart=function(){return j.timeActual=j.timeSet,a=j.timeSet,n(),j},j.pause=function(){return null!=h&&clearInterval(h),j},j.toArray=function(){return a.split(":")},j.toSeconds=function(){return l(a)},j.toMinutes=function(){return Math.floor(parseInt(l(a),10)/60)},j.toHours=function(){return Math.floor(j.toMinutes()/60)},j.diffTime=function(b,c,d){if(!b||b.length<8||3!=b.split(":").length)return null;var e=a;return d===!0&&(e=j.timeSet),c?m(l(b)-l(e)).replace("-",""):m(l(e)-l(b)).replace("-","")},j.pretty=function(){var b="en-US";b="undefined"!=typeof navigator.browserLanguage?navigator.browserLanguage:navigator.language;var c="hour",d="minute",e="second",f="and";"pt-BR"==b&&(c="hora",d="minuto",e="segundo",f="e");var g=a.split(":"),h=g[0]>0?g[0]+" "+(1==g[0]?c:c+"s"):"",i=g[1]>0?g[1]+" "+(1==g[1]?d:d+"s"):"",j=g[2]>0?g[2]+" "+(1==g[2]?e:e+"s"):"";return(h.length>0?h+(i.length>0||j.length>0?" "+f+" ":""):"")+(i.length>0?i+(j.length>0?" "+f+" ":""):"")+j},j.startParam===!0&&n(),delete j.startParam,j}