React Trigger Function After Browser Paints











up vote
0
down vote

favorite












I am building a chess engine using react and javascript and I am trying to implement a feature where the engine makes a move after you make your own move. I have a function "change_states" that changes the states of the chess game whenever a user makes a move. I also have a function "engine_move" which makes an engine move on the board based on the current states of the game.



When I call "engine_move" as either a callback for setState, a function in ComponentDidUpdate, or even requestAnimationFrame the "engine_move" function runs before the changes from the "change_state" function are reflected in the browser. This means that the moved piece disappears for as long as the engine_move function is running and then two moves render on the board immediately (the original players move and the engine move). I can't figure out how to get it so the browser repaints in-between the user move and the engine move. Any help would be greatly appreciated. Thanks!



class Chess extends Component {
constructor(props) {
super(props);
this.state = {
history: [{ position: new Position('white', initialize_engine_board(), [95,
25], [1, 1, 1, 1], 0) }],
drag_end: null,
promotion:{class:'hidden',start: null, end: null, player: null},
status:null
}
}


change_states(history, position, start, end, promotion_piece) {
let possible_moves = legal_moves(position);
let move = create_move(start, end, position, promotion_piece);
let status = null;
if (is_legal(move, possible_moves)) {
let new_position = make_move(position, move);
let new_moves = legal_moves(new_position);

if (new_moves.length === 0) {
status = 'Game Over'
}
this.setState({
history: history.concat([{position: new_position}]),
drag_end: null,
status: status,
});
}
}

engine_move() {
const history = this.state.history.slice();
const position = history[history.length - 1].position;
//Time in milliseconds
let search_time = 1000;
let engine_move = alphabeta_search(position,10,search_time).move;

if (engine_move === null) {
this.setState({
status: 'Game Over',
});
return;
}

let new_position = make_move(position, engine_move);
this.setState({
history: history.concat([{position: new_position}]),
engine_turn: false
});
}









share|improve this question






















  • It is very similar to this question here: stackoverflow.com/questions/50569064/…
    – Chris Atkeson
    Nov 11 at 22:51















up vote
0
down vote

favorite












I am building a chess engine using react and javascript and I am trying to implement a feature where the engine makes a move after you make your own move. I have a function "change_states" that changes the states of the chess game whenever a user makes a move. I also have a function "engine_move" which makes an engine move on the board based on the current states of the game.



When I call "engine_move" as either a callback for setState, a function in ComponentDidUpdate, or even requestAnimationFrame the "engine_move" function runs before the changes from the "change_state" function are reflected in the browser. This means that the moved piece disappears for as long as the engine_move function is running and then two moves render on the board immediately (the original players move and the engine move). I can't figure out how to get it so the browser repaints in-between the user move and the engine move. Any help would be greatly appreciated. Thanks!



class Chess extends Component {
constructor(props) {
super(props);
this.state = {
history: [{ position: new Position('white', initialize_engine_board(), [95,
25], [1, 1, 1, 1], 0) }],
drag_end: null,
promotion:{class:'hidden',start: null, end: null, player: null},
status:null
}
}


change_states(history, position, start, end, promotion_piece) {
let possible_moves = legal_moves(position);
let move = create_move(start, end, position, promotion_piece);
let status = null;
if (is_legal(move, possible_moves)) {
let new_position = make_move(position, move);
let new_moves = legal_moves(new_position);

if (new_moves.length === 0) {
status = 'Game Over'
}
this.setState({
history: history.concat([{position: new_position}]),
drag_end: null,
status: status,
});
}
}

engine_move() {
const history = this.state.history.slice();
const position = history[history.length - 1].position;
//Time in milliseconds
let search_time = 1000;
let engine_move = alphabeta_search(position,10,search_time).move;

if (engine_move === null) {
this.setState({
status: 'Game Over',
});
return;
}

let new_position = make_move(position, engine_move);
this.setState({
history: history.concat([{position: new_position}]),
engine_turn: false
});
}









share|improve this question






















  • It is very similar to this question here: stackoverflow.com/questions/50569064/…
    – Chris Atkeson
    Nov 11 at 22:51













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am building a chess engine using react and javascript and I am trying to implement a feature where the engine makes a move after you make your own move. I have a function "change_states" that changes the states of the chess game whenever a user makes a move. I also have a function "engine_move" which makes an engine move on the board based on the current states of the game.



When I call "engine_move" as either a callback for setState, a function in ComponentDidUpdate, or even requestAnimationFrame the "engine_move" function runs before the changes from the "change_state" function are reflected in the browser. This means that the moved piece disappears for as long as the engine_move function is running and then two moves render on the board immediately (the original players move and the engine move). I can't figure out how to get it so the browser repaints in-between the user move and the engine move. Any help would be greatly appreciated. Thanks!



class Chess extends Component {
constructor(props) {
super(props);
this.state = {
history: [{ position: new Position('white', initialize_engine_board(), [95,
25], [1, 1, 1, 1], 0) }],
drag_end: null,
promotion:{class:'hidden',start: null, end: null, player: null},
status:null
}
}


change_states(history, position, start, end, promotion_piece) {
let possible_moves = legal_moves(position);
let move = create_move(start, end, position, promotion_piece);
let status = null;
if (is_legal(move, possible_moves)) {
let new_position = make_move(position, move);
let new_moves = legal_moves(new_position);

if (new_moves.length === 0) {
status = 'Game Over'
}
this.setState({
history: history.concat([{position: new_position}]),
drag_end: null,
status: status,
});
}
}

engine_move() {
const history = this.state.history.slice();
const position = history[history.length - 1].position;
//Time in milliseconds
let search_time = 1000;
let engine_move = alphabeta_search(position,10,search_time).move;

if (engine_move === null) {
this.setState({
status: 'Game Over',
});
return;
}

let new_position = make_move(position, engine_move);
this.setState({
history: history.concat([{position: new_position}]),
engine_turn: false
});
}









share|improve this question













I am building a chess engine using react and javascript and I am trying to implement a feature where the engine makes a move after you make your own move. I have a function "change_states" that changes the states of the chess game whenever a user makes a move. I also have a function "engine_move" which makes an engine move on the board based on the current states of the game.



When I call "engine_move" as either a callback for setState, a function in ComponentDidUpdate, or even requestAnimationFrame the "engine_move" function runs before the changes from the "change_state" function are reflected in the browser. This means that the moved piece disappears for as long as the engine_move function is running and then two moves render on the board immediately (the original players move and the engine move). I can't figure out how to get it so the browser repaints in-between the user move and the engine move. Any help would be greatly appreciated. Thanks!



class Chess extends Component {
constructor(props) {
super(props);
this.state = {
history: [{ position: new Position('white', initialize_engine_board(), [95,
25], [1, 1, 1, 1], 0) }],
drag_end: null,
promotion:{class:'hidden',start: null, end: null, player: null},
status:null
}
}


change_states(history, position, start, end, promotion_piece) {
let possible_moves = legal_moves(position);
let move = create_move(start, end, position, promotion_piece);
let status = null;
if (is_legal(move, possible_moves)) {
let new_position = make_move(position, move);
let new_moves = legal_moves(new_position);

if (new_moves.length === 0) {
status = 'Game Over'
}
this.setState({
history: history.concat([{position: new_position}]),
drag_end: null,
status: status,
});
}
}

engine_move() {
const history = this.state.history.slice();
const position = history[history.length - 1].position;
//Time in milliseconds
let search_time = 1000;
let engine_move = alphabeta_search(position,10,search_time).move;

if (engine_move === null) {
this.setState({
status: 'Game Over',
});
return;
}

let new_position = make_move(position, engine_move);
this.setState({
history: history.concat([{position: new_position}]),
engine_turn: false
});
}






javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 22:40









Chris Atkeson

143




143












  • It is very similar to this question here: stackoverflow.com/questions/50569064/…
    – Chris Atkeson
    Nov 11 at 22:51


















  • It is very similar to this question here: stackoverflow.com/questions/50569064/…
    – Chris Atkeson
    Nov 11 at 22:51
















It is very similar to this question here: stackoverflow.com/questions/50569064/…
– Chris Atkeson
Nov 11 at 22:51




It is very similar to this question here: stackoverflow.com/questions/50569064/…
– Chris Atkeson
Nov 11 at 22:51

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253969%2freact-trigger-function-after-browser-paints%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253969%2freact-trigger-function-after-browser-paints%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine