You are in the details here and I think trying to do what the code is already trying to do.
The full source with php is
https://github.com/litdev1/maze3D2. VSCode is viewer I reccomend for JS and PHP - can also do limited debugging there or add console.info output and host as a test code somewhere on web.
In particular look at the variables
globalThis.readyToSend and
globalThis.readyToReceive in the JS and the fetch call to php (mode 1) that resets current move (animate=-1) that is confirmed before we poll again (
readyToSend ) or accept a previous return message (
readyToReceive ).
PS
Also note that a consequence of this is that 'get' is not updated until an animation fully completes so if your delay isn't long enough then the get data will not be updated to the new position and you will be sending a command based on the position before the animation finishes.
Bottom line -
need to wait long enough for animation to finish and update new position - no need for animate = -1 or any other actions.
This is my test code based on the partial code you sent -
http://litdev.uk/apps/maze3D/WebAccess.zip note the issues when Sleep is too short appears to be fully consistent with discussion above as far as I can see, and I use the settings name 'Steve'.