Skip to content

Instantly share code, notes, and snippets.

@naosim
Last active August 29, 2015 13:56
Show Gist options
  • Save naosim/8929881 to your computer and use it in GitHub Desktop.
Save naosim/8929881 to your computer and use it in GitHub Desktop.
javascript: (function () {
var TIMER_INPUT_DESCRIPTION = 'Enter a timer.\nex)\n "30" -> 30sec\n "01:20" -> 1min20sec\n "3m" -> 3min';
var END_MESSAGE = 'TIMEUP!!';
var inputSec = function() {
var input = window.prompt(TIMER_INPUT_DESCRIPTION, '60');
if(input.indexOf(':') != -1) {
var a = input.split(':');
return parseInt(a[0], 10) * 60 + parseInt(a[1], 10);
} else if(input.indexOf('m') != -1) {
return parseInt(input.split('m')[0], 10) * 60;
} else if (input.indexOf('s') != -1) {
return parseInt(input.split('s')[0], 10);
} else {
return parseInt(input, 10);
}
};
var defaultTitle = document.title;
var display = function (time) {
if(time <= 0) {
document.title = defaultTitle;
return;
}
sec = Math.floor(time / 1000);
s = sec % 60;
s = s < 10 ? '0' + s : s;
m = Math.floor(sec / 60);
document.title = m + ':' + s;
};
var onEnd = function() {alert(END_MESSAGE);};
var end = inputSec() * 1000 + new Date().getTime();
console.log("aaa");
try{if(pomoloop){clearInterval(pomoloop);}}catch(e){}
pomoloop = setInterval(function () {
var rest = end - new Date().getTime();
display(rest);
if (rest < 0) {
clearInterval(pomoloop);
onEnd();
}
}, 100);
})();
@naosim
Copy link
Author

naosim commented Feb 11, 2014

This is a Timer on TitleBar for Bookmarklet.

Run this program on any web site, and enter a timer.
ex)
"30" -> 30sec
"01:20" -> 1min20sec
"3m" -> 3min

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment