【问题标题】:Get reaction time using angular - call a second function at second click event使用角度获取反应时间 - 在第二次点击事件中调用第二个函数
【发布时间】:2019-09-24 20:27:43
【问题描述】:

我正在尝试在我的 ionic4 应用程序中进行一些反应时间任务。基本上,页面是白色的,当用户准备好点击屏幕(屏幕改变颜色),然后当颜色再次改变时(随机时间后),我想得到时间。

html代码:

<ion-content (click)="changeColor()" >

</ion-content>

.ts 代码

  changeColor() {
    setTimeout(() => {
      this.theme.enableColor('light-theme');
      this.time1 =  new Date().getTime();
    }, this.randomTime);
    this.changeColor2();

  }

  changeColor2() {
    this.theme.enableColor('dark-theme');
  }

主题是我的服务,我在其中创建了 enableColor,它只是一个更改 css 属性的函数。这可以正常工作。

因此,当我单击屏幕时,它会在随机时间后再次更改颜色,然后当我再次单击时,我希望屏幕再次更改颜色并找到用户反应的时间。

【问题讨论】:

    标签: angular ionic4


    【解决方案1】:

    您应该创建两个变量 startTime 和 endTime。
    performance.now() 可能是一个更好的解决方案。

    startTime;
    endTime;
    
    changeColor() {
    setTimeout(() => {
      this.theme.enableColor('light-theme');
    
      const self = this;
      this.endTime = performance.now();
      document.addEventListener('click', function (event) {
        // If the clicked element doesn't have the right selector, bail
        if (!event.target.matches('.scroll-content')) return;
        //event.preventDefault();
        // Log the clicked element in the console
        console.log('changeColor1');
        console.log(self.startTime - self.endTime);
        // because bubbling...
        event.stopImmediatePropagation();
        }, false);
      }, 1000);
    this.changeColor2();
    
    }
    

    changeColor2() 函数

    changeColor2() {
      this.startTime =  performance.now();
      this.theme.enableColor('dark-theme');
      console.log('changeColor2')
    }
    

    我在stackBlitz做了一个简单的例子

    【讨论】:

    • 感谢您的帮助!我怎样才能只允许一次“测试”,所以这意味着点击 2 次,我会得到一个时间值,然后用户无法尝试。因为如果是这样,self.startTime - self.endTime 在每次点击时都会发生变化......我试图将所有代码放在 if 中。如果 (counter === 1) 然后我运行代码并在代码的末尾放置一个 counter = 2,所以通常你不能进入函数内部。它有效,但我得到的时间结果小于 0。@Beller
    • 你应该使用一个标志。
    猜你喜欢
    • 2017-07-03
    • 1970-01-01
    • 2022-01-24
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2016-04-15
    相关资源
    最近更新 更多