[英]Implementing “this is taking too long” message with jQuery
How to implement a gmail-like "this is taking too long" warning message using jQuery Ajax API ? 如何使用jQuery Ajax API实现类似gmail的“这需要太长时间”的警告消息 ?
For those who have never seen this message on gmail, it appears when the 'signing in' process takes too long for completion, and then some solutions are suggested. 对于那些从未在gmail上看过这条消息的人来说,当“登录”过程需要很长时间才能完成时会出现,然后会建议一些解决方案。
I'm using jQuery Ajax on my website and I want to warn users when page loading is very slow and then suggest some solutions (a link to refresh the page, or to a help page, for example). 我在我的网站上使用jQuery Ajax,我想在页面加载非常慢时警告用户然后建议一些解决方案(例如刷新页面或帮助页面的链接)。
I'd suggest something as simple as this arrangement: 我建议像这种安排一样简单:
function tooLong() {
// What should we do when something's taking too long? Perhaps show a `<div>` with some instructions?
$("#this-is-taking-too-long").show();
}
// Then, when you're about to perform an action:
function performSomeAction() {
var timer = setTimeout(tooLong, 10000);
$.get('/foo/bar.php', {}, function() {
// Success!
clearTimeout(timer);
});
}
Why not just use the built-in jQuery ajax 'timeout' option . 为什么不使用内置的jQuery ajax'timeout'选项 。 It's a good practice to use it anyways, in case you have issues with your ajax call.
无论如何,使用它是一个很好的做法,以防你的ajax调用出现问题。 Or, you could re-invent the wheel ;)
或者,你可以重新发明轮子;)
edit: and, er, I think you would want to couple that with an error function. 编辑:呃,我想你想把它与错误函数结合起来。
the jQuery loading plugin i wrote does this as a default option. 我编写的jQuery加载插件将此作为默认选项。 here's the demo:
这是演示:
http://jquery-values.googlecode.com/svn/other/loading/jquery.loading.htm http://jquery-values.googlecode.com/svn/other/loading/jquery.loading.htm
http://plugins.jquery.com/project/loading http://plugins.jquery.com/project/loading
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.