2

Do you know if there is an easy way to pass some arguments to a function called via

haxe.Timer.delay(func, delay);

By "easy" I mean without creating any custom timer.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Johnny Oin
  • 557
  • 5
  • 16

2 Answers2

6

You can use bind() for this. For example, if you want to call someFunction("abc"):

haxe.Timer.delay(someFunction.bind("abc"), 1000); // 1s

Prior to Haxe 3, you could use callback:

haxe.Timer.delay(callback(someFunction,"abc"), 1000); // 1s
Gama11
  • 31,714
  • 9
  • 78
  • 100
Andy Li
  • 5,894
  • 6
  • 37
  • 47
4

Everything can be achieved with an extra level of indirection :-)

It seems like you need a closure whose only job is to call the other function with arguments.

Something like this (untested):

haxe.Timer.delay(function () {
    func(arg1, arg2);
}, delay);
Cameron
  • 96,106
  • 25
  • 196
  • 225
  • Cameron, the only problem with that is it will treat the function as a anonymous function and remove all context. Depending on your implementation this can be a problem. –  Oct 07 '11 at 00:29
  • @user: You're right, but in more recent versions of HaXe I believe the closure captures the full context (`this` and all). – Cameron May 31 '13 at 15:57
  • As of today, `delay` in in milliseconds (eg. `1000` produces a one second delay). – ashes999 May 30 '15 at 16:43