Skip to main content
New Participant
April 30, 2025
Solved

Typescript Async setInterval issue?

Let's say I have an async interval call like so:

this.async.setInterval(()=>{
        console.log(player.name.get())
    },1000)

This code works as intended within the TypeScript environment.

However, the moment I add a variable to capture the timeout ID as mentioned in the documentation:

        /**
         * Sets a timer which executes a function or specified piece of code once the timer expires.
         * @param callback - A function to be compiled and executed after the timer expires.
         * @param timeout - The time, in milliseconds that the timer should wait before the specified function or code is executed.
         * If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle.
         * @param args - Additional arguments which are passed through to the function specified by callback.
         * @returns The timer created by the call to `setTimeout()`.
         * This value can be passed to `clearTimeout()` to cancel the timeout. It is guaranteed that a timeoutID value will never be reused
         * by a subsequent call to setTimeout() or setInterval() on the same object (a window or a worker).
         */

With this code:

id = this.async.setInterval(()=>{
        console.log(player.name.get())
    },1000)

It no longer runs the async interval function and only returns the interval ID. There doesn't seem to be a way to run an interval solely off an interval ID.

Code with side effects is supposed to run regardless. In C, `printf` in an if statement will still run. For example:

#include <stdio.h>
if(printf("Hello")<0){
// error handle
}

 (slightly similar to Let* implicit progn in Common Lisp as well)

SeeingBlue
Honored Contributor
May 1, 2025

You're listening to OnPlayerEnterTrigger twice instead of exit. Tripped me up at first too.

Upcoming Events : Performance Pro - UI Unleashed - Building Responsive & Performant Interfaces Thursday June 19th 5:00 PM PT - Register June Programming Calendar

4 replies

SeeingBlue
Honored Contributor
April 30, 2025

Define id like this, should work. Which article did you use?

import * as hz from 'horizon/core';

class simpleTest extends hz.Component<typeof simpleTest> {
  static propsDefinition = {};

  id: number = 0;

  preStart(): void {
    this.connectCodeBlockEvent(this.entity, hz.CodeBlockEvents.OnPlayerEnterWorld, (player) => {
      this.id = this.async.setInterval(() => {
        console.log(player.name.get())
      }, 1000)
    })
  }

  start() {

  }

}
hz.Component.register(simpleTest);
Upcoming Events : Performance Pro - UI Unleashed - Building Responsive & Performant Interfaces Thursday June 19th 5:00 PM PT - Register June Programming Calendar
whigmalAuthor
New Participant
May 1, 2025

still does not work as intended.
Here is my code:

import * as hz from 'horizon/core';
import {PlayerManager, PlayerInfo, playerInfoString} from "PlayerManager"

class DamageTickZone extends hz.Component<typeof DamageTickZone> {
  static propsDefinition = {
    damage: {type: hz.PropTypes.Number, default: 10}
  };
  id: number = 0;
  preStart() {
    this.connectCodeBlockEvent(this.entity, hz.CodeBlockEvents.OnPlayerEnterTrigger, this.onPlayerEnterTrigger.bind(this));
    this.connectCodeBlockEvent(this.entity, hz.CodeBlockEvents.OnPlayerEnterTrigger, this.onPlayerExitTrigger.bind(this));

}

  start() {}

  onPlayerEnterTrigger(player: hz.Player) {
    const InfoManager = PlayerManager.getInstance(this.world)
    this.id = this.async.setInterval(()=>{
        console.log(player.name.get())
    },1000)

    console.log(this.id)
  }
  onPlayerExitTrigger(player: hz.Player){
    this.async.clearInterval(this.id)
  }
}

hz.Component.register(DamageTickZone);

 However, if I change it to not have the `id` value, the interval will execute:

 onPlayerEnterTrigger(player: hz.Player) {
    const InfoManager = PlayerManager.getInstance(this.world)
    this.async.setInterval(()=>{
        console.log(player.name.get())
    },1000)
  }

I am not using any guide for this. I knew TypeScript already from React (another Meta framework). Self-explanatory to learn from their function docs.

SeeingBlue
Honored Contributor
May 1, 2025

You're listening to OnPlayerEnterTrigger twice instead of exit. Tripped me up at first too.

Upcoming Events : Performance Pro - UI Unleashed - Building Responsive & Performant Interfaces Thursday June 19th 5:00 PM PT - Register June Programming Calendar