Search code examples
javascriptaframe

Showing a text timer that counts seconds in A-frame


So I'm having some trouble getting a timer into my A-frame game. I'm using the component and want it to update every second, like a normal game timer. I can display the text fine, but can't seem to figure out how to measure the game time.

<a-text id="score" value="hits" position="-0.2 -0.6 -1" color="red" width="5" anchor="left"></a-text>
$("#score").setAttribute("text","value","hits "+score)

I update the score text like this and that works, but how to get time from game start and use that to update another ?


Solution

  • Simplest way is through setInterval like:

    var time = 0;
    setInterval(() => {
      timeText.setAttribute('text', 'value', `${time} seconds`);
      time++;
    }, 1000);