[英]Throttling/delaying or other optimizations to avoid stalls/error 500s with jQuery requests
I have a page that is updated using jQuery according to a few actions that a user can carry out. 根据用户可以执行的一些操作,我有一个使用jQuery更新的页面。
I've noticed that requests can stall or the server simply gives an 500 error. 我注意到请求可以停止或服务器只是给出500错误。 This could be an indication that too many requests are taking place simultaneously.
这可能表明同时发生的请求太多。
The following actions can take place: 可以执行以下操作:
One of the things that can happen is that a user can press the next button repeatedly or they action the scroller continously. 可能发生的一件事是用户可以反复按下下一个按钮,或者他们连续操作滚动条。 This in turn can cause a lot of requests to "pile up".
这反过来会导致很多请求“堆积”。
What could I do to delay actions (disable buttons? add a minimal interval between requests? ...) and what other forms of optimizations could be used? 我该怎么做才能延迟操作(禁用按钮?在请求之间添加最小间隔?...)以及可以使用哪些其他形式的优化?
First of all, you can fetch not only next image, but lets say 10 images in one request. 首先,您不仅可以获取下一个图像,还可以在一个请求中提取10个图像。 When the user is repeatedly clicking next, do not fire request on each next.
当用户反复点击下一个时,不要在每个下一个点击请求。 Fire request with setTimeout() of 0.5 seconds, clearing previous timeout.
使用0.5秒的setTimeout()触发请求,清除之前的超时。 You may log how many times the 'next' was clicked in order to retrieve the correct number of images.
您可以记录点击“下一个”的次数,以便检索正确数量的图像。 For the rating, favourite and comment, the logic is perfectly valid, so you should only ensure that those operations on the server side are as fast as possible.
对于评级,收藏和评论,逻辑完全有效,因此您应该只确保服务器端的这些操作尽可能快。
User activates the scrollbar of an infinite scrolling gallery, jQuery monitors the position of the last image and fetches the description of a new random image each time a limit is reached.
用户激活无限滚动图库的滚动条,jQuery监视最后一个图像的位置,并在每次达到限制时获取新随机图像的描述。
Once again, do not fetch them 1 by one, fetch them in groups 再一次,不要逐个获取它们,分组获取它们
You'll want to check this jQuery plugin that throttles - debounces events: 你要检查这个限制的jQuery插件 - 去抖动事件:
http://benalman.com/projects/jquery-throttle-debounce-plugin/ http://benalman.com/projects/jquery-throttle-debounce-plugin/
The idea behind this and behind your needs is caching the user request, and waiting a bit to see if the user changes his mind or ask for more, and then converting all those requests in ONE ajax call. 这背后的想法和你的需求背后的想法是缓存用户请求,等待用户改变主意或要求更多,然后在一个ajax调用中转换所有这些请求。
You could also cache non-live user requests, and commit them only once in a while. 您还可以缓存非实时用户请求,并仅在一段时间内提交一次。 Example:
例:
Another thing: you'll probably want to have a look at your server code. 另一件事:您可能想要查看您的服务器代码。 maybe make use of optimizing stuff like gzipping http requests, using memcached to lighten your database load and response time, etc.
也许利用优化gzipping http请求等优点,使用memcached减轻数据库负载和响应时间等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.