Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Stever1388 opened this issue Jan 9, 2025 · 1 comment · Fixed by #7009
Closed

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

Stever1388 opened this issue Jan 9, 2025 · 1 comment · Fixed by #7009
Assignees

Comments

@Stever1388
Copy link

Stever1388 commented Jan 9, 2025

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.

@zekeatchan
Copy link
Collaborator

Hi @Stever1388. Thanks for submitting this issue. Thanks @samme for creating a PR to fix this issue. It has been merged with the master branch. It will be part of the next release. Do test it out and let us know if you encounter any issues.

@zekeatchan zekeatchan self-assigned this Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants