使用jQuery实现“这需要太长时间”的消息

如何使用jQuery Ajax API实现类似gmail的“这需要太长时间”的警告消息 ?

对于那些从未在gmail上看过这条消息的人来说,当“登录”过程需要很长时间才能完成时会出现,然后会建议一些解决方案。

我在我的网站上使用jQuery Ajax,我想在页面加载非常慢时警告用户然后建议一些解决方案(例如刷新页面或帮助页面的链接)。

我建议像这种安排一样简单:

function tooLong() { // What should we do when something's taking too long? Perhaps show a `
` 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); }); }

为什么不使用内置的jQuery ajax’timeout’选项 。 无论如何,如果你的ajax调用有问题,最好使用它。 或者,你可以重新发明轮子;)

编辑:呃,我想你想把它与错误函数结合起来。