Created
December 4, 2013 09:19
-
-
Save takashi/7784659 to your computer and use it in GitHub Desktop.
timer that works in web worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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