Search code examples
javascriptaframewebvr

Are there ways to stop the execution of a Javascript function until a specific event triggers?


I'm new to JS and doing a project with user-npc interactions using A-Frame with the following scenario:

  1. The user walks to the door;
  2. The user clicks the door;
  3. After the door is open, the npc enters the room.

Is there a way to play each part separately, or only if I call the npc interaction function in the 'animationend' event of the door?

Sorry if any bad english, I'm quite rusty.


Solution

  • Technically you could use await to "pause" the execution of javascript until some Promise is fulfilled, but that's probably an awkward way to go about it. In A-Frame especially, something like this would usually just be done with emitting events after each of those steps and listening to them individually to act on them.