Skip to content

Instantly share code, notes, and snippets.

@takashi
Created December 4, 2013 09:19
Show Gist options
  • Save takashi/7784659 to your computer and use it in GitHub Desktop.
Save takashi/7784659 to your computer and use it in GitHub Desktop.
timer that works in web worker
var Timer = function() {
onmessage = this.switchCase.bind(this);
this.currentTime = 0;
this.timerId = 0;
};
p = Timer.prototype;
p.switchCase = function(e) {
switch(e.data) {
case 0:
this.stopTimer();
break;
case 1:
this.startTimer();
break;
case 2:
this.getTime();
break;
}
};
p.stopTimer = function() {
clearInterval(this.timerId);
this.currentTime = 0;
this.timerId = 0;
postMessage(0);
};
p.startTimer = function() {
this.timerId = setInterval(function() {
this.currentTime++;
}.bind(this), 1000)
};
p.getTime = function() {
postMessage(this.currentTime);
};
new Timer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment