有条件地暂停Javascript以等待ajax

有条件地暂停Javascript以等待ajax,ajax,jquery-ui,jquery,Ajax,Jquery Ui,Jquery,变量ajaxdata在success函数中被修改,如果还没有修改,我想等待2秒钟,然后不修改它继续 该用例用于jqueryui自动完成字段。自动完成源是一个ajax请求,但是如果用户快速键入,并在加载列表之前退出该字段,则该字段将保持未设置状态。使用“自动完成”上的“更改”事件,我会检查用户是否输入了有效的选项,但如果在更改事件触发时未加载源,则这不起作用。因此,如果源(存储在变量“ajaxdata”中)为空,我想在change函数中设置一个等待的延迟 代码: input.autocomplet

变量ajaxdata在success函数中被修改,如果还没有修改,我想等待2秒钟,然后不修改它继续

该用例用于jqueryui自动完成字段。自动完成源是一个ajax请求,但是如果用户快速键入,并在加载列表之前退出该字段,则该字段将保持未设置状态。使用“自动完成”上的“更改”事件,我会检查用户是否输入了有效的选项,但如果在更改事件触发时未加载源,则这不起作用。因此,如果源(存储在变量“ajaxdata”中)为空,我想在change函数中设置一个等待的延迟

代码:

input.autocomplete({
来源:功能(请求、响应){
$.ajax(
{
键入:“获取”,
url:“/some/url”,
数据类型:“json”,
成功:功能(数据){
响应($.map)(数据、功能(项){
返回{
标签:item.label,
值:item.value
}
}));
ajaxdata=数据;
}
}
);
//ajaxopts=ajaxsource(请求、响应、ajaxurl、xtraqry)
},                   
更改:功能(事件、用户界面){
如果(!ui.item){
//用户未选择选项,但键入的内容可能仍然匹配
var enteredString=$(this.val();
var stringMatch=false;
if(ajaxdata.length==0){
///这就是我需要2秒延迟的地方
}
var opts=ajaxdata;
对于(变量i=0;i
编辑:


更具体地说,这个问题是:这个延迟需要有条件的。这意味着,如果数据已经加载(因为它来自静态源,或者来自早期的ajax调用),我不希望有延迟。

我不确定您想做什么,但我非常确定这样做会更好:

input.autocomplete({
    source: function(request, response) {
        return $.ajax({
            type: "GET",
            url: "/some/url",
            dataType: "json"
        });
    },
    change: function(event, ui) {
        if (!ui.item) {
            // user didn't select an option, but what they typed may still match
            var enteredString = this.value;
            var stringMatch = false;
            //make sure ajax is complete
            this.source().done(function(data) {
                var opts = $.map(data, function(item) {
                    return {
                        label: item.label,
                        value: item.value
                    }
                });
                for (var i = 0; i < opts.length; i++) {
                    if (opts[i].label.toLowerCase() == enteredString.toLowerCase()) {
                        $(this).val(opts[i].label); // corrects any incorrect case
                        stringMatch = true;
                    }
                }
            });
        }
    }
});​
input.autocomplete({
来源:功能(请求、响应){
返回$.ajax({
键入:“获取”,
url:“/some/url”,
数据类型:“json”
});
},
更改:功能(事件、用户界面){
如果(!ui.item){
//用户未选择选项,但键入的内容可能仍然匹配
var enteredString=this.value;
var stringMatch=false;
//确保ajax是完整的
this.source().done(函数(数据){
var opts=$.map(数据、函数(项){
返回{
标签:item.label,
值:item.value
}
});
对于(变量i=0;i
如果我正确理解您的意思,我想您只是想检查一下是否已填充了
ajaxdata
;但如果没有,只需再等两秒钟,然后不带它继续

试试这个:

change: function(event, ui) {
    if (!ui.item) {
        // user didn't select an option, but what they typed may still match

        if (ajaxdata.length==0){
            ///  THIS IS WHERE I NEED A 2 SECOND DELAY

            //pass in 'this' so that you can use it
            setTimeout(function() {correctCase(this);}, 2000);
        }       
    }
}

function correctCase(inThis){       

    //I'm not sure what this variable does.  do you really need it???   
    var stringMatch = false;

    var enteredString = $(inThis).val();
    //you still want to be sure that ajaxdata is not empty here
    if (ajaxdata.length==0){
        var opts = ajaxdata;
        for (var i=0; i < opts.length; i++){
            if(opts[i].label.toLowerCase() == enteredString.toLowerCase()){
                $(inThis).val(opts[i].label);   // corrects any incorrect case
                stringMatch = true;  //this variable doesn't seem to do anything after this???
                break;
            }
        }
    }
}
function correctCase(本例){
//我不确定这个变量的作用。你真的需要它吗???
var stringMatch=false;
var enteredString=$(inThis.val();
//您仍然需要确保ajaxdata在这里不是空的
if(ajaxdata.length==0){
var opts=ajaxdata;
对于(变量i=0;i
默认情况下,JavaScript在遇到异步函数时是异步的,它会将该函数排队等待以后使用。 但是,如果您想要暂停js(ajax调用或任何东西),您可以使用承诺 案例1:输出hello(不会等待setTimeout)

案例2:输出done1(将等待setTimeout)


您不需要两秒钟的延迟(顺便说一句,这可以通过setTimeout轻松实现),您需要等待ajax调用完成!您是否尝试过在发送之前使用
完成:
设置
$.ajax()
?您可以在发送HTTP请求之前和完成HTTP请求时绑定要执行的函数。请参阅,这可以帮助您实现您想要实现的目标。您已经特意描述了用例,但不清楚。我最初的设计并没有等到ajax完成,因为如果ajax请求花费的时间太长,它依赖于其他一些代码。这可能会改变。似乎setTimeout应该很容易实现,但我很难让它像我认为的那样工作,请举个例子。谢谢你的建议。目前,这是
function correctCase(inThis){       

    //I'm not sure what this variable does.  do you really need it???   
    var stringMatch = false;

    var enteredString = $(inThis).val();
    //you still want to be sure that ajaxdata is not empty here
    if (ajaxdata.length==0){
        var opts = ajaxdata;
        for (var i=0; i < opts.length; i++){
            if(opts[i].label.toLowerCase() == enteredString.toLowerCase()){
                $(inThis).val(opts[i].label);   // corrects any incorrect case
                stringMatch = true;  //this variable doesn't seem to do anything after this???
                break;
            }
        }
    }
}
//async 
function myFunction() {
let result1='hello'
//promise =new Promise((resolve,reject)=>{
setTimeout(function(){ 
resolve("done");
result1="done1";
}, 3000);
//});
 //result = await promise
 alert(result1);
}
myFunction();
async function myFunction() {
let result1='hello'
promise =new Promise((resolve,reject)=>{
setTimeout(function(){ 
resolve("done");
result1="done1";
}, 3000);
});
 result = await promise
 alert(result1);
}
myFunction();