如何在“后台”中运行javascript函数/没有冻结UI [英] How to run javascript function in "background" / without freezing UI

查看:110
本文介绍了如何在“后台”中运行javascript函数/没有冻结UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一个HTML表单,它在很多不同的标签中都有很多问题(来自数据库)。然后用户在这些问题中给出答案。每次用户更改选项卡时,我的Javascript都会创建一个保存。问题是每次更改标签时我都要遍历所有问题,每次冻结表格大约5秒钟。

I've done an HTML form which has a lot of questions (coming from a database) in many different tabs. User then gives answers in those questions. Each time a user changes a tab my Javascript creates a save. The problem is that I have to loop through all questions each time the tab is changed and it freezes the form for about 5 seconds every time.

我一直在寻找我可以在后台运行保存功能的答案。显然没有真正的方法在后台运行某些东西,很多人建议使用 setTimeout(); 例如这个如何让一组js函数在后台运行

I've been searching for an answer how I can run my save function in the background. Apparently there is no real way to run something in the background and many recommend using setTimeout(); For example this one How to get a group of js function running in background

但是这些例子都没有解释或考虑到即使我使用像 setTimeout(saveFunction,2000); 这样的东西也没解决我的问题。在这种情况下,它只会将它推迟2秒。

But none of these examples does explain or take into consideration that even if I use something like setTimeout(saveFunction, 2000); it doesn't solve my problem. It only postpones it by 2 seconds in this case.

有没有办法解决这个问题?

Is there a way to solve this problem?

推荐答案


显然没有真正的方法可以在后台运行...

Apparently there is no real way to run something on background...

大多数现代浏览器(但不是IE9及更早版本):网络工作者

There is on most modern browsers (but not IE9 and earlier): Web Workers.

但我认为你是试图在错误的级别上解决问题:1。应该可以在很多小于5秒的时间内遍历所有控件,并且2.它不应该是必要的在其中只有一个更改时循环遍历所有控件。

But I think you're trying to solve the problem at the wrong level: 1. It should be possible to loop through all of your controls in a lot less than five seconds, and 2. It shouldn't be necessary to loop through all controls when only one of them has changed.

我建议在尝试将处理卸载到后台之前查看这些问题。

I suggest looking to those problems before trying to offload that processing to the background.

例如,您可以拥有一个包含每个对象的当前值的对象em,然后让每个项目的UI在值更改时更新该对象。然后你就拥有了该对象中的所有值,而无需再次遍历所有控件。

For instance, you could have an object that contains the current value of each item, and then have the UI for each item update that object when the value changes. Then you'd have all the values in that object, without having to loop through all the controls again.

这篇关于如何在“后台”中运行javascript函数/没有冻结UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆