javascript 点击onClick처리
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 点击onClick처리相关的知识,希望对你有一定的参考价值。
import React, { Component } from "react";
const rspCoords = {
바위: "0",
가위: "-142px",
보: "-284px"
};
const scores = {
가위: 1,
바위: 0,
보: -1
};
const computerChoice = imgCoord => {
return Object.entries(rspCoords).find(function(v) {
return v[1] === imgCoord;
})[0];
};
export default class RSP extends Component {
state = {
result: "",
imgCoord: rspCoords.바위,
score: 0
};
interval;
componentDidMount() {
this.interval = setInterval(this.changeHand, 100);
}
componentWillUnmount() {
clearInterval(this.interval);
}
changeHand = () => {
const { imgCoord } = this.state;
if (imgCoord === rspCoords.바위) {
this.setState({
imgCoord: rspCoords.가위
});
} else if (imgCoord === rspCoords.가위) {
this.setState({
imgCoord: rspCoords.보
});
} else if (imgCoord === rspCoords.보) {
this.setState({
imgCoord: rspCoords.바위
});
}
};
onClickBtn = choice => () => {
const { imgCoord } = this.state;
clearInterval(this.interval);
const myScore = scores[choice];
const cpuScore = scores[computerChoice(imgCoord)];
const diff = myScore - cpuScore;
if (diff === 0) {
this.setState({
result: "비겼습니다"
});
} else if ([-1, 2].includes(diff)) {
this.setState(prevState => {
return {
result: "이겼습니다",
score: prevState.score + 1
};
});
} else {
this.setState(prevState => {
return {
result: "졌습니다",
score: prevState.score - 1
};
});
}
setTimeout(() => {
this.interval = setInterval(this.changeHand, 100);
}, 1000);
};
render() {
const { result, score, imgCoord } = this.state;
return (
<>
<div
id="computer"
style={{
background: `url(https://en.pimg.jp/023/182/267/1/23182267.jpg) ${imgCoord} 0`
}}
/>
<div>
<button id="rock" className="btn" onClick={this.onClickBtn("바위")}>
바위
</button>
<button
id="scissor"
className="btn"
onClick={this.onClickBtn("가위")}
>
가위
</button>
<button id="paper" className="btn" onClick={this.onClickBtn("보")}>
보
</button>
</div>
<div>{result}</div>
<div>현재 {score}점</div>
</>
);
}
}
以上是关于javascript 点击onClick처리的主要内容,如果未能解决你的问题,请参考以下文章
javascript promise(비동기처리)
markdown 에러처리#python #tutorial
markdown 在tenary operator中抛出错误(삼항연산자예외처리)
javascript 소녀나라스타일 - 갤러리위젯(리뷰작성일노출)
javascript 리뷰평점및설설치
javascript javascript - frequency(배열에서같은갯수를뽑아서배열로리턴)