I wanted a more JavaScript friendly call that wraps the timed code like so:

Callback invocation

123

agent.timedEvent("openCrust 2.0.27",function(){openCrust({});});

This has the benefit of wrapping a desired event – there is no explicit need for anyone to code the ending – it is automatically done via the timedEvent call after invoking the passed in function.

The Titanium module documentation is a bit hard to find (that is, finding up-to-date valid documentation is challenging); your best bet to see how to do something interesting is to look at the various code repositories on Github followed by studying the API docs (i.e. JavaDocs and .h/.m files for iOS).

It turns out, invoking a JavaScript callback in either Android or iOS is fairly straightforward. In the case of Android, you need to use the KrollFunction type like so:

As you can see in the above code, I’m not doing anything special like passing in any arguments to the KrollFunction instance. If you want to do that, say in the case of passing in some special value that the corresponding callback will use, then you can either pass in a Map or an Object[].

For example, you can implement this style of callback where a custom value is passed in for a timed event like so:

As you can see in the above code, a callback instance takes as a parameter an NSArray, thus, I have to covert my NSDictionary into an array via Objective-C’s handy arrayWithObjects function.

The default module examples provided by Appcelerator naturally work, but alas, the non-callback style of invocation was less than appealing, especially if you are going to be coding an Appcelerator app in JavaScript. Nevertheless, you can do it easily enough provided you are willing to dig through myriad repositories…or you could save yourself the headache and read this blog post.