Skip to content

A TimerEvent with a delay of 0 and repeat of -1 causes an infinite loop #7004

Closed
@Stever1388

Description

@Stever1388

Version

  • Phaser Version: 3.86.0
  • Operating system: Windows
  • Browser: Chrome

Description

Setting the delay property to 0 and the repeat property to -1 in an TimerEvent causes an infinite loop.

Example Test Code

Note - the following code will cause your browser tab to lock up.

export default class TimerEventDelayIssue extends Phaser.Scene {
  create() {
    this.time.addEvent({
      delay: 0,
      callback: () => console.log('timer event callback'),
      repeat: -1
    });
  }
}

Additional Information

Prior to Phaser ~3.60, using a delay value of 0 seemed to be a way to "allow this to run every frame." Post Phaser 3.60, there seems to be logic to make sure the callback gets called (on average) once every delay value. So the callback might get called multiple times per frame. This happens here in the Clock.js file. There is some logic in the TimerEvent reset function to check for delay of 0 and loop/repeats, but it doesn't check for repeat === -1. I think it may make more sense for the check to be in Clock.js, in the update loop - something like this:

// Very short delay
if (remainder >= event.delay && event.delay > 0)
{
  while ((remainder >= event.delay) && (event.repeatCount > 0))
  {
    if (event.callback)
    {
      event.callback.apply(event.callbackScope, event.args);
    }

    remainder -= event.delay;
    event.repeatCount--;
  }
}

This allows you to have repeating frame based events just like you could in Phaser 3.55.2, while also protecting against delays that are 0 to prevent infinite loops. The check in TimerEvent.js could then be removed since it would be a valid config again.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions