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

fjs*_*jsj 5 javascript ajax jquery

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

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

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

Vot*_*ple 14

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

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);
    });
}
Run Code Online (Sandbox Code Playgroud)