forked from Falc/Tock.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tock.min.js
3 lines (3 loc) · 2.88 KB
/
tock.min.js
1
2
3
// Tock.js (version 1.0) https://github.com/Falc/Tock.js
// License: MIT
var Tock=function(a){var a=a||{};this.countdown=a.countdown||false;this.start_time=a.start_time||0;this.interval=a.interval||10;this.on_tick=a.on_tick||function(){console.warn('Callback function for "on_tick" is not defined.')};this.on_complete=a.on_complete||function(){console.warn('Callback function for "on_complete" is not defined.')};this.is_running=false;this.timeout=null;this.stopped_time=this.start_time;this.started_at=0;this.time=0;this.elapsed="0.0"};Tock.prototype={start:function(){if(this.is_running===true){return}if(this.countdown===true&&(this.stopped_time-this.time<0)){return}var a=this;this.started_at=Date.now();this.time=0;this.elapsed="0.0";this.is_running=true;this.timeout=window.setTimeout(function(){a.tick()},this.interval)},stop:function(){if(this.is_running===false){return}this.stopped_time=this.lap();this.is_running=false;window.clearTimeout(this.timeout)},reset:function(){this.is_running=false;window.clearTimeout(this.timeout);this.timeout=null;this.stopped_time=this.start_time;this.started_at=0;this.time=0;this.elapsed="0.0"},tick:function(){var c=this;this.time+=this.interval;this.elapsed=Math.floor(this.time/this.interval)/10;if(Math.round(this.elapsed)===this.elapsed){this.elapsed+=".0"}var d=(Date.now()-this.started_at)-this.time;var b=this.interval-d;if(this.on_tick!==undefined){this.on_tick()}if(this.countdown===true&&(this.stopped_time-this.time<0)){this.is_running=false;this.on_complete()}if(b<=0){var a=Math.floor(Math.abs(b)/this.interval);this.time+=a*this.interval;if(this.is_running===true){this.tick()}}else{if(this.is_running===true){this.timeout=window.setTimeout(function(){c.tick()},b)}}},lap:function(a){if(this.is_running===false){if(this.started_at===0){return this.format(this.start_time,a)}return this.format(this.stopped_time,a)}else{if(this.countdown===true){var b=this.stopped_time-(Date.now()-this.started_at);if(b<0){b=0}return this.format(b,a)}else{return this.format(this.stopped_time+(Date.now()-this.started_at),a)}}},format:function(b,l){if(l==undefined||l=="{L}"){return b}var f=l;var a={};var j=b;var c=j%1000;a.L=j.toString();a.LL="000".substring(0,3-j.toString().length)+j.toString();a.l=c.toString();a.ll="000".substring(0,3-c.toString().length)+c.toString();j=(j-c)/1000;var k=j%60;a.S=j.toString();a.SS="00".substring(0,2-j.toString().length)+j.toString();a.s=k.toString();a.ss="00".substring(0,2-k.toString().length)+k.toString();j=(j-k)/60;var d=j%60;a.M=j.toString();a.MM="00".substring(0,2-j.toString().length)+j.toString();a.m=d.toString();a.mm="00".substring(0,2-d.toString().length)+d.toString();var h=(j-d)/60;a.H=h.toString();a.HH="00".substring(0,2-h.toString().length)+h.toString();a.h=a.H;a.hh=a.h;var g=["HH","H","hh","h","MM","M","mm","m","SS","S","ss","s","LL","L","ll","l"];for(var e=g.length-1;e>=0;e--){f=f.replace(new RegExp("{"+g[e]+"}","g"),a[g[e]])}return f}};