jquery - 延迟 ajaxStart n 秒

标签 jquery ajax

如何将 ajaxStart()ajaxStop() 延迟 n 秒,以便每个人都可以看到我的动画 gif。否则它消失得太快了。请指教。谢谢。

$(document).ajaxStart(function() {
    $('#container').append("<div class='progress'></div>");//
}).ajaxStop(function(){
    $('.progress').remove();
});

.progress {
    position: fixed;
    top: 35%;
    left: 45%;
    width: 50%;
    height: 50%;
    background-image: url('/images/progress.gif');
    background-repeat: no-repeat;
    background-size: 10%;
    z-index: 9999;
}

$('#settings-commit').click(function(event) {
    event.preventDefault();

    $.ajax({
        type    : 'POST',
        url : ..............,
        data    : { ............ },
        dataType: 'json',
        success : function(data) {
            if (data.status == 'ok') {
                ...................
            } else {
                ...................
            }
        },
        error   : function() {
                ...................
        }
    });
});

最佳答案

您可以使用超时来做到这一点

var progressTimer;
$(document).ajaxStart(function () {
    $('#container').append("<div class='progress'></div>");
    //if a new ajax request is started then don't remove the progress icon
    clearTimeout(progressTimer);
}).ajaxStop(function () {
    progressTimer = setTimeout(function () {
        $('.progress').remove();
    }, 1000)
});

关于jquery - 延迟 ajaxStart n 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085834/

相关文章:

php - 客户端-服务器架构决策

javascript - jquery 显示提示功能无法在基于 webkit 的浏览器中工作

javascript - 在 meteor 表格中使用集合助手

javascript - 如何做出我自己的选择

ajax - 优雅降级 - 何时考虑

jquery - ASP.NET PageMethods 和 JQuery AJAX Post – 它到底有多安全?

javascript - 使用 DataTables 在服务器端模式下具有单独搜索输入的多个表

javascript - 转换 3D 问题

javascript - 同时将事件绑定(bind)到多个 jquery 元素

java - 使用JSP、servlet和ajax上传文件而不设置表单的action属性