Pass arguments to a delayed function with Haxe
Asked Answered
T

2

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.

Trend answered 17/6, 2010 at 15:47 Comment(0)
S
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
Supraliminal answered 19/6, 2010 at 7:27 Comment(0)
D
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);
Digged answered 18/6, 2010 at 1:55 Comment(3)
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.Gredel
@user: You're right, but in more recent versions of HaXe I believe the closure captures the full context (this and all).Digged
As of today, delay in in milliseconds (eg. 1000 produces a one second delay).Biflagellate

© 2022 - 2024 — McMap. All rights reserved.