AJAX调用未完成时停止执行JS函数

Stop JS function execution while an AJAX call is not finished

本文关键字:执行 JS 函数 调用 未完成 完成时 AJAX      更新时间:2023-09-26

我写了一个小函数来动态包含CSS文件或JS文件。我用jquery函数$.getScript()加载JS文件。

我想在AJAX调用完成之前阻止函数返回。

我试过这样的东西,但不起作用:

CAPTIVEA.resources._includeResource = function(resource) {
    var ret = false;
    if (CAPTIVEA.resources.isIncluded(resource))
    {   // Resource already included => do nothing
        console.log('CAPTIVEA.resources : "' + resource + '" is already included.');
        ret = true;
    }
    else
    {   // Resource not included => try to include it
        var resType = CAPTIVEA.resources.getType(resource);
        switch (resType)
        {   // Check resource type to know how to include it
            case 'js':
                $.when(
                    $.getScript(resource, function(data, textStatus){
                        console.info('CAPTIVEA.resources : "' + resource + '" dynamically loaded.'); //success
                    })
                ).done(function(){
                    ret = true;
                });
                break;
            case 'css':
                // Load CSS file ...
                break;
            default:
                console.error('CAPTIVEA.resources : "' + resource + '" is not an includable resource.');
                break;
        }
    }
    return ret;
}

您可以使用$.ajax并将async设置为false,以便它在继续之前等待响应:

$.ajax({
   url: resource,
   success: function(data, textStatus) {
                        console.info('CAPTIVEA.resources : "' + resource + '" dynamically loaded.'); //success
   },
   dataType: 'script',
   async: false
});

使用通常不需要同步调用。AJAX的全部意义在于它是异步的。第一个A代表异步

为什么需要阻止函数返回?通常,这是因为您希望在加载时使用资源(在本例中是脚本)。你必须使用回调:

错误:

$.getScript(resource);
doSomethingWithResource();

右:

$.getScript(resource, function() {
  doSomethingWithResource();
});

试试这个:

CAPTIVEA.resources._includeResource = function(resource) {
    var ret = false;
    if (CAPTIVEA.resources.isIncluded(resource))
    {   // Resource already included => do nothing
        console.log('CAPTIVEA.resources : "' + resource + '" is already included.');
        ret = true;
    }
    else
    {   // Resource not included => try to include it
        var resType = CAPTIVEA.resources.getType(resource);
        successFlag = false;
        switch (resType)
        {   // Check resource type to know how to include it
            case 'js':
                jQuery.ajax({
                    async:false,
                    type:'POST',
                    url: resource,
                    data: null,
                    success: function(){successFlag =true;}
                });
                break;
            case 'css':
                // Load CSS file ...
                break;
            default:
                console.error('CAPTIVEA.resources : "' + resource + '" is not an includable resource.');
                break;
        }
        ret = successFlag;
    }
    return ret;
}

我认为这是唯一的解决方案,尽管我们必须添加一个全局变量(successFlag)。另一种解决方案是在成功回调时做你必须做的事情,但这样你的函数就不会像你预期的那样工作。