电池友好的方式来检测文本框的内容是否已更改

发布于 2024-12-28 15:45:30 字数 442 浏览 2 评论 0原文

此问题的答案建议使用 window.setInterval 是立即检测(无需等待模糊)文本框内容是否更改的唯一可靠方法,因为某些文本框更改可能通过按键以外的事件发生。

即使在我的台式电脑上,我也看到 IE 或 Firefox 通过 500 毫秒的 setInterval 运行一个相对简单的处理程序检查几个文本框,从而消耗了相当大的 CPU 百分比。这在台式电脑上没问题,但在笔记本电脑和手机上会损害电池寿命。

使用 jquery 最大限度地减少 CPU 利用率但仍然提供快速更新以响应文本框的更改(包括剪贴板粘贴、ajax 更新等,而不仅仅是按键)的好方法是什么?

This question's answers propose using window.setInterval as the only reliable way to immediately detect (without waiting for blur) if a text box's content is changed, given that some textbox changes can happen via events other than key presses.

Even on my desktop PC I see IE or Firefox chewing up a non-trivial percentage of CPU with a 500 msec setInterval running with a relatively simple handler checking a few textboxes. This is OK on a desktop PC but on laptops and mobiles it will hurt battery life.

What's a good way using jquery to minimize CPU utilization but still provide fast updates in response to changes (including clipboard pasting, ajax updates, etc. not just key presses) to a textbox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

无言温柔 2025-01-04 15:45:30

onBlur 是指您离开某个对象但不一定对其进行更改。

onChange 仅在您离开已更改值的对象后才会调用。

其中之一应该适合您的需求。如果没有,每次字符更改都会触发各种按键事件。

如果不这样做,那么定期更新单个 JavaScript 变量可能不会像您所说的那样占用那么多处理能力,除非您的选择器导致一些奇怪的 DOM 行为。

试试这个:通过 id 引用元素,并将该引用存储在 js 变量中。通过此引用更新和更改另一个变量是一个微不足道的操作。

onBlur is when you have moved away from an object without necessarily changing it.

onChange is only called after you have moved away from an object that you have changed the value of.

One of these should fit your needs. If not, the various keypress events will trigger for each character change.

If these don't then regularly updating a single javascript variable could not possibly take as much processing power as you are saying UNLESS your selector is causing some odd DOM behavior.

Try this: Reference the element by id, and store that reference in a js variable. Updating and changing another variable via this reference is then a trivial operation.

末骤雨初歇 2025-01-04 15:45:30

开始在该文本框的焦点上运行 window.setInterval (或 setTimeout)检查器,并在模糊时终止它。这样,当光标不在文本框中时,您就不会耗尽 CPU 功率。

Start running your window.setInterval (or setTimeout) checker on focus of that text box, and kill it on blur. That way you won't be using up CPU power when the cursor isn't in the text box.

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