启动计时器>在keyDown上记录startTime>然后记录keyDown持续时间

时间:2018-02-24 04:03:30

标签: javascript rxjs keyevent

我尝试为游戏控制器设计回放录音机。在游戏开始(),我需要一个计时器来启动。当keyDown事件发生时,我需要记录它发生的时间。之后,记录时间直到键被释放以获得keyDown持续时间。

目前

<script type="text/javascript">
    const keyDown$ = Rx.Observable.fromEvent(document, "keydown")
      .filter(e => !e.repeat)

    const keyUp$ = Rx.Observable.fromEvent(document, "keyup");

    keyDown$
      .mergeMap(keyDown => // For every key down we start a new observable, waiting for the-same-key up event
        keyUp$.filter(keyUp => keyUp.keyCode === keyDown.keyCode)
        .take(1)
        .map(keyUp => ({
          keyCode: keyUp.keyCode,
          key: keyUp.key,
          duration: keyUp.timeStamp - keyDown.timeStamp
        }))
      .scan((actions, action) => { // The scan collects all the actions aggregated in a map by key. It emits the current aggregated map for every action
        actions[action.key] = actions[action.key] || {};
        const actionIndex = Object.keys(actions[action.key]).length;
        actions[action.key][actionIndex] = `${Math.round(action.duration)}ms`;
        return actions;
      }, {})
      .subscribe(console.log)
</script>
input: hold w 5 seconds
       hold w 3 seconds
       hold d 2 second
output: {
          'w': {0: 5000ms, 1: 3000ms},
          'd': {0: 2000ms}
        } 

目标

我想创建全局计时器。然后当发生按键事件时,记录startTime。之后,记录时间直到钥匙被释放。

<div>
    <button onclick="start()">Start</button>    
</div>          
<script type="text/javascript">
    var _startTime = 0;

    function start(){

        _startTime = performance.now();
        console.log(_startTime);
    }

    const keyDown$ = Rx.Observable.fromEvent(document, "keydown")
      .filter(e => !e.repeat)

    const keyUp$ = Rx.Observable.fromEvent(document, "keyup");


    keyDown$
      .mergeMap(keyDown =>
        keyUp$.filter(keyUp => keyUp.keyCode === keyDown.keyCode)
        .take(1)
        .map(keyUp => ({
          keyCode: keyUp.keyCode,
          key: keyUp.key,
          startTime: keyDown.timeStamp - _startTime, // Here get time from start
          duration: keyUp.timeStamp - keyDown.timeStamp
        })

        )
      )
      .scan((actions, action) => {
        actions[action.key] = actions[action.key] || {};
        const actionIndex = Object.keys(actions[action.key]).length;
          //code after this isn't legit/working
        actions[action.key][actionIndex][startTime] = `${Math.round(action.startTime)}ms`;
        actions[action.key][actionIndex][duration] = `${Math.round(action.duration)}ms`;
        return actions;
      }, {})
      .subscribe(console.log)

</script>  
..Expected data
input: press start 
       hold w 5 seconds
       hold w 3 seconds
       hold d 2 second
output:     {
              'w': {
                     0: {
                          startTime: 21ms,
                          duration: 5000ms
                         } 
                     1: {
                          startTime: 446ms,
                          duration: 3000ms
                         } 
              'a': {
                     0: {
                          startTime: 965ms,
                          duration: 2000ms
                         } 
            } 


Error : Uncaught ReferenceError: startTime is not defined
    at actions[action.key][actionIndex][startTime] = `${Math.round(action.startTime)}ms`;
请等一下

1 个答案:

答案 0 :(得分:0)

解决:

 id1= id2=id3=.......=True
 c="id"
 for(int i=1;i<=100;i++)
  {
    c=c+str(i)
    if(check the value of id's are true)
    }