Guest User

Untitled

a guest
Feb 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. <script type="text/javascript">
  2. const keyDown$ = Rx.Observable.fromEvent(document, "keydown")
  3. .filter(e => !e.repeat)
  4.  
  5. const keyUp$ = Rx.Observable.fromEvent(document, "keyup");
  6.  
  7. keyDown$
  8. .mergeMap(keyDown => // For every key down we start a new observable, waiting for the-same-key up event
  9. keyUp$.filter(keyUp => keyUp.keyCode === keyDown.keyCode)
  10. .take(1)
  11. .map(keyUp => ({
  12. keyCode: keyUp.keyCode,
  13. key: keyUp.key,
  14. duration: keyUp.timeStamp - keyDown.timeStamp
  15. }))
  16. .scan((actions, action) => { // The scan collects all the actions aggregated in a map by key. It emits the current aggregated map for every action
  17. actions[action.key] = actions[action.key] || {};
  18. const actionIndex = Object.keys(actions[action.key]).length;
  19. actions[action.key][actionIndex] = `${Math.round(action.duration)}ms`;
  20. return actions;
  21. }, {})
  22. .subscribe(console.log)
  23. </script>
  24.  
  25. input: hold w 5 seconds
  26. hold w 3 seconds
  27. hold d 2 second
  28. output: {
  29. 'w': {0: 5000ms, 1: 3000ms},
  30. 'd': {0: 2000ms}
  31. }
  32.  
  33. <div>
  34. <button onclick="start()">Start</button>
  35. </div>
  36. <script type="text/javascript">
  37. var_startTime = 0;
  38.  
  39. function start(){
  40.  
  41. _startTime = performance.now();
  42. console.log(_startTime);
  43. }
  44.  
  45. const keyDown$ = Rx.Observable.fromEvent(document, "keydown")
  46. .filter(e => !e.repeat)
  47.  
  48. const keyUp$ = Rx.Observable.fromEvent(document, "keyup");
  49.  
  50.  
  51. keyDown$
  52. .mergeMap(keyDown =>
  53. keyUp$.filter(keyUp => keyUp.keyCode === keyDown.keyCode)
  54. .take(1)
  55. .map(keyUp => ({
  56. keyCode: keyUp.keyCode,
  57. key: keyUp.key,
  58. startTime: keyDown.timeStamp - _startTime, // Here get time from start
  59. duration: keyUp.timeStamp - keyDown.timeStamp
  60. })
  61.  
  62. )
  63. )
  64. .scan((actions, action) => {
  65. actions[action.key] = actions[action.key] || {};
  66. const actionIndex = Object.keys(actions[action.key]).length;
  67. //code after this isn't legit/working
  68. actions[action.key][actionIndex][startTime] = `${Math.round(action.startTime)}ms`;
  69. actions[action.key][actionIndex][duration] = `${Math.round(action.duration)}ms`;
  70. return actions;
  71. }, {})
  72. .subscribe(console.log)
  73.  
  74. </script>
  75.  
  76. input: press start
  77. hold w 5 seconds
  78. hold w 3 seconds
  79. hold d 2 second
  80. output: {
  81. 'w': {
  82. 0: {
  83. startTime: 21ms,
  84. duration: 5000ms
  85. }
  86. 1: {
  87. startTime: 446ms,
  88. duration: 3000ms
  89. }
  90. 'a': {
  91. 0: {
  92. startTime: 965ms,
  93. duration: 2000ms
  94. }
  95. }
  96.  
  97.  
  98. Error : Uncaught ReferenceError: startTime is not defined
  99. at actions[action.key][actionIndex][startTime] = `${Math.round(action.startTime)}ms`;
Add Comment
Please, Sign In to add comment