Mojo SDK - 设置计时器

发布于 2024-07-29 01:59:20 字数 296 浏览 5 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月牙弯弯 2024-08-05 01:59:20

事实证明,Mojo 使用的是原型 javascript 框架。

我能够通过使用以下方法解决这个问题:

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

让我困惑的一件事是 delay 方法更改了 this 的值,因此延迟函数一定不能期望 < code>this 将与您直接直接调用它相同。

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.

无人问我粥可暖 2024-08-05 01:59:20

@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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文