简体   繁体   English

如何在JavaScript中刷新屏幕数据而不会使页面变慢?

[英]How to refresh screen data in JavaScript without making the page slow?

I have a question in terms of code and NOT user experience, I have the JS: 我有一个关于代码而不是用户体验的问题,我有JS:

$(document).on( "click", "input:radio,input:checkbox", function() {
        getContent($(this).parent(),0);
    });

The above JS gets the contents from radios and checkboxes, and it refreshes the page to show dependencies. 上面的JS从单选和复选框获取内容,并刷新页面以显示依赖关系。 For example if I check on yes, and the dependency is on yes, show text box, the above works! 例如,如果我选择“是”,并且依赖项是“是”,则显示文本框,以上方法有效!

What I want to know is, if there is a better way to do the same thing, but in a more friendly way, as this is at times, making the pages slow. 我想知道的是,是否有更好的方法来做同样的事情,但是以更友好的方式做,这有时会使页面变慢。 Especially if I do a lot of ticks/checks in one go, I miss a few, as the parent refreshes! 尤其是如果我一口气做很多的滴答/检查,我会错过一些,因为父母会刷新!

If you have to hit your server to getContent() then it will automatically be slow. 如果必须将服务器连接到getContent() ,它将自动变慢。

However, you can save a lot if you send all the elements once instead of hitting the server each time a change is made. 但是,如果只发送一次所有元素,而不是每次进行更改都需要访问服务器,则可以节省很多。

Yet, if creating one super large page is not an option, then you need to keep your getContent() function, but there is one possible solution, in case you did not already implement such, which is to cache all the data that you queried earlier. 但是,如果不能创建一个超大页面,则需要保留getContent()函数,但是有一种可能的解决方案,以防您尚未实现,即缓存所有查询的数据。早。

So you could have an object (a map) which has keys defining the data you're interested in. If the key is defined, then the data is already available and your return and use that data directly from the cache. 因此,您可以有一个对象(一个地图),该对象的键定义了您感兴趣的数据。如果定义了键,则该数据已经可用,您可以直接从缓存中返回并使用该数据。 Otherwise, you have to hit the server. 否则,您必须打服务器。

One thing to do, you mentioned slowness as you 'tick' things back and forth, is to not send more than one request at a time to the server (with a timeout in case the server never replies). 您提到的一件事是,在来回“打勾”时很慢,一次不要向服务器发送一个以上的请求(如果服务器永不答复,则超时)。 So the process here is: 所以这里的过程是:

  1. Need data 'xyz' 需要数据“ xyz”
  2. Is that data already cached? 该数据是否已经缓存? if yes, then skip step (3 and 4) 如果是,则跳过步骤(3和4)
  3. If a request being worked on? 是否正在处理请求? if yes, push the data on the request stack and return 如果是,则将数据压入请求堆栈并返回
  4. Send a request to the server, which blocks any further request until answer for 'xyz' is received 向服务器发送请求,服务器将阻止任何其他请求,直到收到“ xyz”的答案为止
  5. Receive the answer and cache the data in an object (map) and release the request queue 接收答案并将数据缓存在对象(映射)中,然后释放请求队列
  6. Make use of data as required 根据需要使用数据
  7. I check the request queue, if not empty pop the next request and start processing from (2) 我检查请求队列,如果不为空,则弹出下一个请求并从(2)开始处理

The request process is expected to be run on a timer because (1) it can time out and (2) it need to run in the background (not GUI preemptive) 该请求进程预计将在计时器上运行,因为(1)它可能会超时,并且(2)它需要在后台运行(不是GUI抢先)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM