调用 10 个 API 同时保持良好用户体验的最佳解决方案?
我有一个类似于 Expedia 和 Priceline 的网站。用户输入日期,然后调用 10 个不同的 API 来返回结果。从所有 10 个结果中获取结果可能(有时)最多需要 90 秒。
我想要的是在用户等待结果时显示启动/加载屏幕(类似于 Priceline)。
我尝试使用 jQuery Ajax 调用 API 并生成结果,但某些浏览器遇到问题(当结果不够快时锁定)。
我在 LAMP 环境中,我想过使用 cronjob 来处理 API 并将结果保存在数据库中,但我不确定这有多实用。
I have a site similar to Expedia and Priceline. The user enters dates and then 10 different APIs are called to return results. Getting results from all 10 can (at times) take up to 90 seconds.
What I'd like to have is the splash/loading screens (similar to Priceline) to display while the user waits for results.
I have tried using jQuery Ajax to call the APIs and generate results, but some browsers experience issues (locking up when results aren't fast enough).
I'm on a LAMP environment and I've thought of using a cronjob to process the APIs and save the results in a database, but I'm not sure how practical that would be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过类似 beanstalkd 的东西?似乎如果延迟长达 90 秒,您应该对作业进行排队并在结果准备就绪时通知用户,而不是同步运行作业并显示长时间运行的启动页面。要实现推送通知,请尝试 jQuery Comet。
Have you tried something like beanstalkd? It seems like if the latency is up to 90 seconds, you should queue up the jobs and notify the user when the result is ready rather than run the jobs synchronously and display a long-running splash page. To implement push notification, try jQuery Comet.
如果您需要一次显示所有结果,请触发对 php 页面的单个调用,并让它通过curl 进行调用。但请记住设定时间限制。
否则,将工作分成两到四个页面,并对每个页面触发 ajax 调用。当每个人返回时,显示这些结果。在这种情况下,一个简单的加载图像就足够了。这里没有溅水。
并记住尽可能缓存。
***编辑
请注意,我在这里重点关注 php 执行 api 调用。您的服务器及其 T1+ 连接肯定会比我的拨号更快地处理 10 个呼叫。 :)。
If u need to show all results at once, fire a single call to a php page and let it do the calls via curl. Remeber set time limit though.
Else, split the work up between two to four pages and fire ajax calls to each. As each returns, show those results. In this case, a simple loading image will suffice. No splash here.
And remember to cache what you can.
***Edit
Note that I'm focusing on php doing the api calls here. Your server with its T1+ connection is definitely going to handle 10 calls faster than my dialup. :).