X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fpomo.git;a=blobdiff_plain;f=pomo.js;fp=pomo.js;h=88646f7e494bf9d43c958add42d6db1d47baffbb;hp=0000000000000000000000000000000000000000;hb=7e874b3d502201f8e2382958e0f6f37c507579e9;hpb=7e6c7cffe88d8ebbb4d2566bdebe26265505fd4a diff --git a/pomo.js b/pomo.js new file mode 100644 index 0000000..88646f7 --- /dev/null +++ b/pomo.js @@ -0,0 +1,46 @@ +MILS_PER_SECOND = 1000; +MILS_PER_MINUTE = 60 * MILS_PER_SECOND; +MILS_PER_HOUR = 60 * MILS_PER_MINUTE; + +function Pomo() { + var p = this; + this.pomo = document.getElementById("pomo"); + this.start = function() { + this.date = new Date(); + setTimeout(function() { + p.update(); + }, 500); + }; + this.prefix = function(i) { + if (i < 10) { + i = "0" + i; + } + return i; + } + this.update = function() { + var now = new Date(); + var mils = this.date.getTime() + 25 * MILS_PER_MINUTE - now.getTime(); + if (mils < 0) { + this.alarm(); + mils = 0; + } else { + setTimeout(function() { + p.update(); + }, 500); + } + var hours = Math.floor(mils / MILS_PER_HOUR); + mils -= hours * MILS_PER_HOUR; + var minutes = Math.floor(mils / MILS_PER_MINUTE); + mils -= minutes * MILS_PER_MINUTE; + var seconds = Math.floor(mils / MILS_PER_SECOND); + var s_date = this.prefix(hours) + ":" + this.prefix(minutes) + ":" + this.prefix(seconds); + this.pomo.innerHTML = s_date; + }; + this.alarm = function() { + var audio = document.getElementById("alarm"); + audio.play(); + } +} + +var pomo = new Pomo(); +pomo.start();