Question

I'm messing around with building an application for the Palm Pre.

I have a simple question: How can I set up a timer for some code to get run after a certain amount of time has passed?

I tried using the regular old javascript setTimeout, but it doesn't seem to work.

Here is what I've tried:

setTimeout(this.someFunction, 3000);
setTimeout('this.someFunction()', 3000);

Neither one seems to work. How can I accomplish this?

Was it helpful?

Solution

Turns out that the prototype javascript framework is used by Mojo.

I was able to solve this issue by using:

this.someFunction.delay(seconds, [functionArgs,]);

One thing that tripped me up was that the delay method changed the value of this, so the delayed function must not expect that this will be the same as if you had simply invoked it directly.

OTHER TIPS

@TM: Thanks for pointing out Prototype's bind() method. I was struggling with the setTimeout() problem yesterday and ended up using Prototype's delay() method like you pointed out, and then this morning I saw in Mitch Allen's "Palm webOS" book that he was calling setTimeout() on the this.controller.window object, like so:
this.controller.window.setTimeout(this.someFunction.bind(this), someNumberOfMilliseconds);

I don't think I would have noticed the use of this.controller.window if I had not been looking for exactly that solution, and now I'm noticing several places in the book where this.someFunction.bind(this) is used, although he never explains what that does. Now I know!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top