Fantasy 8Bit system (F8), is a fantasy 8bit console and a set of libraries for creating fantasy 8bit consoles.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
642B

  1. class Clock{
  2. constructor(){
  3. this.__ticktime = 1000;
  4. this.__dt = 0;
  5. this.__ltime = 0;
  6. this.__out = [];
  7. }
  8. get hz(){return Math.floor(1000 / this.__ticktime);}
  9. set hz(hz){
  10. if(hz >= 1)
  11. this.__ticktime = 1000 / hz;
  12. }
  13. clk(fn){
  14. if (typeof(fn) === 'function')
  15. this.__out.push(fn);
  16. }
  17. tick(){
  18. let ctime = (new Date()).getTime();
  19. if(this.__ltime !== 0){
  20. this.__dt += ctime - this.__ltime;
  21. while(this.__dt > this.__ticktime){
  22. this.__out.forEach((fn)=>{fn();});
  23. this.__dt -= this.__ticktime
  24. }
  25. }
  26. this.__ltime = ctime;
  27. }
  28. }
  29. module.exports = Clock;