12

Is there an implementation of setTimeout() and clearTimeout() in Haxe? It's of course possible to use the Timer class, but for a one-shot execution it's not the best way, I guess.

Gama11
  • 31,714
  • 9
  • 78
  • 100
nepa
  • 1,421
  • 2
  • 18
  • 28

2 Answers2

18

For a one-shot execution I think that Timer.delay() is perfect. You can use the returned instance to stop the timer later:

var timer = haxe.Timer.delay(function() trace("Hello World!"), 250);
...
timer.stop();

You could also access the native setTimeout() with the js.html.Window extern:

var handle = js.Browser.window.setTimeout(function() trace("Hello World!"), 250);
...
js.Browser.window.clearTimeout(handle);
Gama11
  • 31,714
  • 9
  • 78
  • 100
Franco Ponticelli
  • 4,430
  • 1
  • 21
  • 24
2

In case you're using the framework:

Kha modifies haxe.Timer to call kha.Scheduler, which in the end doesn't get the timestamps via setTimeout - it gets these via requestAnimationFrame().

This seems to not work while a tab is inactive, so it's not the same function while the tab is inactive.

I'm attempting a workaround, but at the moment, it doesn't give the same result as a native setTimeout()-JS does (although I found a workaround which I'll present for inclusion).

Gama11
  • 31,714
  • 9
  • 78
  • 100