AJAX查询处于活动状态时禁用窗口大小调整

Disabling window resize when AJAX query active

本文关键字:窗口大小 调整 活动状态 查询 AJAX      更新时间:2023-12-15

我有一个jQuery脚本,它是一个搜索脚本,但也包含在调整窗口大小时将元素调整为(屏幕高度-40px)的功能。但是,当搜索(AJAX查询)处于活动状态时,我想禁用调整大小功能。有人知道我该怎么做吗

我当前的代码是:

$(document).ready(function(){
    $(window).resize(function(){
        if($(window).height()<1200){
            $("#a").height($(window).height()-40);
        }
    });
    $("form").submit(function(a){
        a.preventDefault();
        if($("#i").val().length>0){
            $.ajax({
                type:"get",
                url:"search.php?q="+q,
                dataType:"html",
                success:function(a){
                    ...
                }
            })
        }
    })
})

使用.on()和.of()

$(document).ready(function(){
    function started(){
        if($(window).height()<1200){
            $("#a").height($(window).height()-40);
        }
    $(window).on("resize.name_space",started);
    $("form").submit(function(a){
        a.preventDefault();
        if($("#i").val().length>0){
           $(window).off("resize.name_space");
            $.ajax({
                type:"get",
                url:"search.php?q="+q,
                dataType:"html",
                success:function(a){
                $(window).on("resize.name_space",started);    ...
                }
            })
        }
    })
})
var loading;
...
    if($(window).height()<1200 && !loading){
...
        loading = true;
        $.ajax({
            ...
            complete: function(){
                loading = false;
            }.
        })
    }
})

试试这个,它在ajax运行时添加了一个签入,并将阻止进行重新调整大小

$(document).ready(function(){
//Create the variable
var check = false;
$(window).resize(function(){
    //Is the ajax currently running? This if statement runs if the answer is no
    if (check == false && $(window).height()<1200 ) {
        $("#a").height($(window).height()-40);
    }
});
$("form").submit(function(a){
    a.preventDefault();
    //Set the variable to true, this stops the resizing above ^^
    check = true;
    if($("#i").val().length>0){
        $.ajax({
            type:"get",
            url:"search.php?q="+q,
            dataType:"html",
            success:function(a){
                ...
            },
            complete: function() {
                //Set back to off once the ajax has finished
                check = false;
            }
        })
    }
})

})