Pass arguments to a delayed function with Haxe

旧城冷巷雨未停 提交于 2019-12-10 23:28:56

问题


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.


回答1:


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



回答2:


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);


来源:https://stackoverflow.com/questions/3063286/pass-arguments-to-a-delayed-function-with-haxe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!