gpt4 book ai didi

javascript - 如何仅在浏览器 Javascript 上完成另一个功能后执行一个功能?

转载 作者:行者123 更新时间:2023-12-04 07:40:02 25 4
gpt4 key购买 nike

我想创建一个 Web 应用程序国际象棋游戏,所以每个棋子都会有一个事件监听器,等待玩家点击它。当玩家点击一个棋子时(现在我只使用 pawns)它的 id 将被保存并打印出来,只有在此之后所有方块才应该等待点击(我不使用有效方块,可以是任何正方形);如果点击一个方块,它的 id 应该被保存并打印出来。
但是在控制台上,当我单击一个 pawn 时,它的 id 会打印两次,一次用于正常打印,另一次用于方形打印(所以它采用了方形打印)。因此,不是程序等待调用在单击块后打印方块 id 的函数,而是程序在方块 id 的位置打印块 id。所以我认为程序不会等到第一个函数完成:

//Each div(square) of chess board

var $class_white = [...document.querySelectorAll('.house_white')];
var $class_black = [...document.querySelectorAll('.house_black')];

//Every pawn

var $class_pawn = [...document.querySelectorAll('.pawn')];

//Variables to store the piece and square id.
var $piece;
var $square;


//Every pawn is waiting to be clicked
function $$wait_pawns(item) {
item.addEventListener('click', $$pawn_clicked);
}

//This will add the event listener type 'click' to the squares
function $$prepare_squares(){
$class_white.forEach($$wait_squares);
$class_black.forEach($$wait_squares);
}

//If a pawn is clicked, it will save the pawn id and then call $$prepare_squares() to add an event listener type 'click' to the squares
function $$pawn_clicked(event) {
$piece = event.target.id;
console.log($piece);
$$prepare_squares();
}

//Now every square is wainting to be clicked
function $$wait_squares(item){
item.addEventListener('click', $$square_clicked);
}

//After a square is clicked, it's id will be saved and will be printed.
function $$square_clicked(event2){
$square = event2.target.id;
console.log($square);
}

//This will add an event listener type 'click' to all pawns
$class_pawn.forEach($$wait_pawns);

This is an screenshot of the console and chess board

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
/*background-color: rgb(0,0,80);*/
background-image: url("black.png");
background-size: cover;
background-attachment: fixed;
background-repeat: no-repeat;
}

.pawn{
height:50px;
width:50px;
}

.bishop{
height:50px;
width:50px;
}

.rock{
height:50px;
width:50px;
}

.horse{
height:50px;
width:50px;
}

#king{
height:50px;
width:50px;
}


#queen{
height:60px;
width:60px;
}

.container {
display: grid;
grid-template-columns: repeat(8, 1fr);
grid-template-rows: repeat(8, 1fr);
width: 600px;
height: 600px;
top: 50%;
left: 50%;
position: absolute;
transform: translate(-50%,-50%);
/*border-bottom: 9px solid rgb(0,0,0);
border-left: 9px solid rgb(0,0,0);
border-top: 9px solid rgb(0,0,0);
border-right: 9px solid rgb(0,0,0);*/
}

.house_white {
display: flex;
background-color: rgb(250,250,250);
align-items: center;
justify-content: center;
}

.house_black {
display: flex;
background: rgb(0,100,0);
align-items: center;
justify-content: center;
}
#demo {
padding-bottom: 2px;
height:50px;
width:220px;
display: flex;
justify-content: center;
align-items: center;
color: rgb(255,255,255);
position: absolute;
top: 30%;
left: 90%;
transform: translate(-50%,-50%);
border-radius: 8px;
font-family: Arial, sans-serif, serif;
font-size: 1.30rem;
border-bottom: 2px solid rgb(200,200,200);
}

#start {
height:50px;
width:200px;
display: flex;
justify-content: center;
align-items: center;
background-color: rgb(0,0,0);
color: rgb(255,255,255);
position: absolute;
top: 60%;
left: 90%;
transform: translate(-50%,-50%);
border-radius: 8px;
font-family: Arial, sans-serif, serif;
font-size: 1.30rem;
outline: none;
}
#start:hover {
background-color: rgb(255,255,255);
color: rgb(0,0,0);
}
#circle {
height:30px;
width:30px;
}

#queen {
height:40px;
width:40px;
}

/*#joke {
height:300px;
width:300px;
position: absolute;
top: 40%;
left: 15%;
transform: translate(-50%,-50%);
border-radius: 8px;
border: 6px solid;
border-color: rgb(0,0,0);
}*/
<!DOCTYPE html>
<html lang="en-US">

<head>
<meta charset="UTF-8">
<meta name="description" content="Dynamic, light, fast and simple">
<meta name="author" content="necromancerc-137">
<meta name="keywords" content="dynamic">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Javascript testing</title>
</head>

<body>
<main>
<!--<img id="joke" src="joke.gif">-->
<div id="demo">Frontend...</div>
<button id="start">Help!</button>
<div class="container">
<div id = "a8" class="house_white"></div>
<div id = "b8" class="house_black"></div>
<div id = "c8" class="house_white"></div>
<div id = "d8" class="house_black"></div>
<div id = "e8" class="house_white"></div>
<div id = "f8" class="house_black"></div>
<div id = "g8" class="house_white"></div>
<div id = "h8" class="house_black"></div>

<div id = "a7" class="house_black"></div>
<div id = "b7" class="house_white"></div>
<div id = "c7" class="house_black"></div>
<div id = "d7" class="house_white"></div>
<div id = "e7" class="house_black"></div>
<div id = "f7" class="house_white"></div>
<div id = "g7" class="house_black"></div>
<div id = "h7" class="house_white"></div>

<div id = "a6" class="house_white"></div>
<div id = "b6" class="house_black"></div>
<div id = "c6" class="house_white"></div>
<div id = "d6" class="house_black"></div>
<div id = "e6" class="house_white"></div>
<div id = "f6" class="house_black"></div>
<div id = "g6" class="house_white"></div>
<div id = "h6" class="house_black"></div>

<div id = "a5" class="house_black"></div>
<div id = "b5" class="house_white"></div>
<div id = "c5" class="house_black"></div>
<div id = "d5" class="house_white"></div>
<div id = "e5" class="house_black"></div>
<div id = "f5" class="house_white"></div>
<div id = "g5" class="house_black"></div>
<div id = "h5" class="house_white"></div>

<div id = "a4" class="house_white"></div>
<div id = "b4" class="house_black"></div>
<div id = "c4" class="house_white"></div>
<div id = "d4" class="house_black"></div>
<div id = "e4" class="house_white"></div>
<div id = "f4" class="house_black"></div>
<div id = "g4" class="house_white"></div>
<div id = "h4" class="house_black"></div>

<div id = "a3" class="house_black"></div>
<div id = "b3" class="house_white"></div>
<div id = "c3" class="house_black"></div>
<div id = "d3" class="house_white"></div>
<div id = "e3" class="house_black"></div>
<div id = "f3" class="house_white"></div>
<div id = "g3" class="house_black"></div>
<div id = "h3" class="house_white"></div>

<div id = "a2" class="house_white"><img id="pawn0" class="pawn" src="pawn.png"></div>
<div id = "b2" class="house_black"><img id="pawn1" class="pawn" src="pawn.png"></div>
<div id = "c2" class="house_white"><img id="pawn2" class="pawn" src="pawn.png"></div>
<div id = "d2" class="house_black"><img id="pawn3" class="pawn" src="pawn.png"></div>
<div id = "e2" class="house_white"><img id="pawn4" class="pawn" src="pawn.png"></div>
<div id = "f2" class="house_black"><img id="pawn5" class="pawn" src="pawn.png"></div>
<div id = "g2" class="house_white"><img id="pawn6" class="pawn" src="pawn.png"></div>
<div id = "h2" class="house_black"><img id="pawn7" class="pawn" src="pawn.png"></div>

<div id = "a1" class="house_black"><img class="rock" src="rock.png"></div>
<div id = "b1" class="house_white"><img class="horse" src="horse.png"></div>
<div id = "c1" class="house_black"><img class="bishop" src="bishop.png"></div>
<div id = "d1" class="house_white"><img id="queen" src="queen.png"></div>
<div id = "e1" class="house_black"><img id="king" src="king.png"></div>
<div id = "f1" class="house_white"><img class="bishop" src="bishop.png"></div>
<div id = "g1" class="house_black"><img class="horse" src="horse.png"></div>
<div id = "h1" class="house_white"><img class="rock" src="rock.png"></div>
</div>
</main>
<script type="text/javascript" src="test.js"></script>
</body>
</html>

最佳答案

So I think the programm is not waiting until the first function is finished


这不是正在发生的事情。非 async JavaScript 函数 总是 运行至完成(除非整个环境终止,例如关闭网页)。
单击事件从子元素传播(“冒泡”)到其父元素再到其父元素等。由于您的 pawn 是方块的子元素,因此在处理 pawn 上的 click 事件后,单击事件会冒泡到方块并在那里处理。
您需要做几件事:
  • 使用 event.stopPropagation(); 防止事件冒泡在 event对象在 $$pawn_clicked
  • 让变量存储你的程序的状态信息,这样它就知道它是否希望在一个方块上获得一次点击。我认为您在 $piece 中已经有了它。多变的。所以你的方块点击处理程序应该只在 $piece 时做一些事情不是“无件”值(看起来您正在使用 undefined)。
  • 关于javascript - 如何仅在浏览器 Javascript 上完成另一个功能后执行一个功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67532418/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com