【问题标题】:Ajax settimout when checking if file exists检查文件是否存在时的Ajax settimeout
【发布时间】:2016-01-25 11:44:42
【问题描述】:

我正在检查文件是否存在。使用 1 秒间隔进行检查。 我想要实现的目标:如果 10 秒后找不到文件,则发出警报。 我试图在间隔内设置超时,但没有成功。 任何提示都会很棒,在此先感谢。

var isLoading=new Boolean();
isLoading=false;
setInterval(
function(){                      
    $.ajax({
        url: ajaxRequestUrl,
        type: "GET",
        cache: false,                                  
        statusCode: {
            // HTTP-Code "Page not found"
            404: function() {
                if (isLoading===false){
                    do_this();
                }
            },
            // HTTP-Code "Success"
            200: function() {
                if (isLoading===true){
                    do_that();
                }

            }    
        }
    });     
},
1000);

【问题讨论】:

  • 定义一个var counter = 0并在ajax调用中增加1。当该值达到10 时,显示您的警报。
  • 很棒的小费。如果文件仍然不存在,让我显示警告/警报并在后台继续检查。

标签: javascript ajax settimeout


【解决方案1】:

我将建议以下代码:

var isLoading=new Boolean();
isLoading=false;
var isFileFound=false;

$.ajax({
    url: ajaxRequestUrl,
    type: "GET",
    cache: false,                                  
    statusCode: {
        // HTTP-Code "Page not found"
        404: function() {
            if (isLoading===false){
                do_this();
            }
        },
        // HTTP-Code "Success"
        200: function() {
    isFileFound=true;
            if (isLoading===true){
                do_that();
            }

        }    
    }
});     

setTimeout(function(){
alert(isFileFound);
},10000);

【讨论】:

    【解决方案2】:

    你需要一个变量在一秒钟后检查。

    var isLoading=new Boolean();
    isLoading=false;
    var tick = 0;
    var getFile = setInterval(function(){
        if(tick == 0) {
            $.ajax({
                url: ajaxRequestUrl,
                type: "GET",
                cache: false,                                  
                statusCode: {
                    // HTTP-Code "Page not found"
                    404: function() {
                        if (isLoading===false){
                            do_this();
                        }
                    },
                    // HTTP-Code "Success"
                    200: function() {
                        if (isLoading===true){
                            do_that();
                        }
                    }    
                }
            });
        }
        else if(tick == 10) {
            clearInterval(getFile);
            alert("File not found");
            tick = 0;
        }
        else {
            tick++;
        }
    }, 1000);
    

    在间隔中我检查tick是否等于0(不要多次发出相同的请求),如果是,我会发出请求,否则等待。 10 秒后显示警报,然后将刻度设置为 0。在示例中,我还清除了不继续迭代的间隔。这样你就可以请求一个新文件了。

    【讨论】:

    • 干净而出色的解决方案。非常感谢。
    • 我认为你应该在alert()之前clearInterval(),因为如果用户等待1秒点击警报的OK按钮,代码会引发另一个警报等等......我是但是,不能 100% 确定 JavaScript 代码不会在警报引发时暂停。
    • @DineiA.Rockenbach 你是对的。好观察!我更新了我的代码
    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多