编程语言
首页 > 编程语言> > javascript – 使用setTimeout模拟并行操作有什么本质上的错误吗?

javascript – 使用setTimeout模拟并行操作有什么本质上的错误吗?

作者:互联网

当此应用的用户对字段进行更改时,需要在其他字段中进行大量更改.通常,即使使用优化的脚本,浏览器也会在IE中阻止用户输入超过1秒.为了阻止发生,我这样做:

var i = 100;
GetTextInputs().filter('[' + name + ']').each(function()
{    
    setTimeout("DoWork('" + this.id + "', '" + v + "', '" + name + "');", i);
    i += 25;
});

这对我来说有些惹人注意,但效果很好.

>这种方法有什么问题吗?
>或者,有更好的方法吗?

解决方法:

可能出现可怕错误的一件事是,拥有太多高频定时器可能[讽刺的是]使得ui迟缓/反应迟钝.从http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series-using.html开始:

With low-frequency timers — timers
with a delay of one second or more —
we could create many timers without
significantly degrading performance on
either device. Even with 100 timers
scheduled, our app was not noticeably
less responsive. With high-frequency
timers, however, the story was exactly
the opposite. A few timers firing
every 100-200 ms was sufficient to
make our UI feel sluggish.

标签:javascript,blocking,user-experience
来源: https://codeday.me/bug/20190527/1160294.html