Mojo SDK - 设置计时器
我正在为 Palm Pre 构建一个应用程序。
我有一个简单的问题:如何设置一个计时器,让某些代码在经过一定时间后运行?
我尝试使用常规的旧 JavaScript setTimeout
,但它似乎不起作用。
这是我尝试过的:
setTimeout(this.someFunction, 3000);
setTimeout('this.someFunction()', 3000);
似乎都不起作用。 我怎样才能做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,Mojo 使用的是原型 javascript 框架。
我能够通过使用以下方法解决这个问题:
让我困惑的一件事是
delay
方法更改了this
的值,因此延迟函数一定不能期望 < code>this 将与您直接直接调用它相同。Turns out that the prototype javascript framework is used by Mojo.
I was able to solve this issue by using:
One thing that tripped me up was that the
delay
method changed the value ofthis
, so the delayed function must not expect thatthis
will be the same as if you had simply invoked it directly.@TM:感谢您指出Prototype 的bind() 方法。 昨天我在 setTimeout() 问题上苦苦挣扎,最终像你指出的那样使用了 Prototype 的 delay() 方法,然后今天早上我在 Mitch Allen 的“Palm webOS”书中看到他在 this.controller 上调用 setTimeout() .window 对象,如下所示:
this.controller.window.setTimeout(this.someFunction.bind(this), someNumberOfMilliseconds);
如果我有的话,我想我不会注意到 this.controller.window 的使用并没有一直在寻找确切的解决方案,现在我注意到书中的几个地方使用了 this.someFunction.bind(this) ,尽管他从未解释过它的作用。 现在我明白了!
@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!