简体   繁体   English

限制/延迟或其他优化以避免jQuery请求的停顿/错误500

[英]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: 可以执行以下操作:

  • User hits rating button for an image, jQuery updates the new rating. 用户点击图像的评级按钮,jQuery更新新的评级。
  • User hits next button, jQuery fetches the description in JSON of a new random image, jQuery updates a few html attributes to show the new image. 用户点击下一个按钮,jQuery在JSON中获取新随机图像的描述,jQuery更新几个html属性以显示新图像。
  • 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监视最后一个图像的位置,并在每次达到限制时获取新随机图像的描述。
  • User clicks a thumbnail, jQuery updates the attributes to show a full size image. 用户单击缩略图,jQuery更新属性以显示完整大小的图像。
  • User enters text in a field to post a comment, hits button, jQuery updates the database. 用户在字段中输入文本以发布评论,点击按钮,jQuery更新数据库。
  • User hits favourite button, jQuery updates database with the user's favourites. 用户点击收藏夹按钮,jQuery用用户的收藏夹更新数据库。
  • Adding to this, url rewriting to redirect image requests to an image-resizing script which takes the original image and resizes it according to a maximum height/width and then caches it in a directory. 除此之外,url重写以将图像请求重定向到图像大小调整脚本,该脚本获取原始图像并根据最大高度/宽度调整其大小,然后将其缓存在目录中。

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: 例:

  • second 0 - User hit "favourite" 第二个0 - 用户点击“最爱”
  • second 30 - User posts a comment 第二个30 - 用户发表评论
  • second 100 - your script takes all requests and send them to the server. 第二个100 - 您的脚本接收所有请求并将它们发送到服务器。

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.

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