Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
54 views
in Technique[技术] by (71.8m points)

Loop in while Angular

I've got a loop in console that prints always start, this is my typescript code. I've got an array of 96 random numbers and every number represent a test, in this code there is only one example but at the end I will have a big switch case with every number. In this application I will do many cognitive test and I need at least two timer for everyone.

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-arrows-test-real',
  templateUrl: './arrows-test-real.component.html',
  styleUrls: ['./arrows-test-real.component.scss']
})

export class ArrowsTestRealComponent implements OnInit {

  a = [];

  plus: boolean;
  counterPlus!: number;
  button: boolean;
  answer!: number;
  userAnswer!: string;
  counterAnswer!: number;
  cue1: boolean;
  cue2: boolean;

  constructor() {
    this.cue1 = false;
    this.cue2 = false;
    this.answer = 1.7;
    this.plus = true;
    this.button = false;

    for (let i = 0; i < 96; i++) {
      // @ts-ignore
      this.a[i] = i;
    }
    console.log(this.a);
    this.shuffle(this.a);
    console.log(this.a);
  }

  ngOnInit(): void {
    while (this.a.length !== 0){
      console.log(this.a[0]);
      const r = this.getRandomTime(0.4, 1.6) + 1;
      if (this.a[0] === 20){
       this.startTimerCongruentLeftUp(r);
      }else {
        console.log(this.a[0]);
        this.a.splice(0, 1);
      }
    }
  }

  // tslint:disable-next-line:typedef
  getRandomInt(min: number, max: number) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  // tslint:disable-next-line:typedef
  shuffle(array: number[]) {
    // tslint:disable-next-line:one-variable-per-declaration
    let currentIndex = array.length, temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;

      // And swap it with the current element.
      temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }

    return array;
  }

  // tslint:disable-next-line:typedef
  getRandomTime(min: number, max: number) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  // tslint:disable-next-line:typedef
  rightButton(){
    this.counterAnswer = -1;
    this.userAnswer = 'right';
  }

  // tslint:disable-next-line:typedef
  leftButton(){
    this.counterAnswer = -1;
    this.userAnswer = 'left';
  }

  // tslint:disable-next-line:typedef
  startTimerCongruentLeftUp(seconds: number) {
    console.log('start');
    let plus = seconds;

    const interval = setInterval(() => {
      console.log(plus);
      // @ts-ignore
      plus = plus - 0.1;

      // @ts-ignore
      if (plus < 0 ) {
        clearInterval(interval);
        this.startTimerAnswerCongruentLeftUp(this.answer);
      }
    }, 100);
  }

  // tslint:disable-next-line:typedef
  startTimerAnswerCongruentLeftUp(seconds: number) {
    this.counterAnswer = seconds;

    const img = document.getElementById('arrow') as HTMLImageElement;
    img.src = 'assets/images/congruentLeft.png';
    const congruent = 'left';
    this.button = true;

    const interval = setInterval(() => {
      console.log(this.counterAnswer);
      // @ts-ignore
      this.counterAnswer = this.counterAnswer - 0.1;
      // @ts-ignore
      if (this.counterAnswer < 0 ) {
        clearInterval(interval);
        // tslint:disable-next-line:no-shadowed-variable
        if (this.userAnswer === congruent){
          alert('right answer');
        }else {
          alert('wrong answer');
        }
        // tslint:disable-next-line:no-shadowed-variable
        const img = document.getElementById('arrow') as HTMLImageElement;
        img.src = '';
        this.button = false;
        this.a.splice(0, 1);
      }
    }, 100);
  }
}
question from:https://stackoverflow.com/questions/65911312/loop-in-while-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...