Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在ajax完成之前停止对象方法调用_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 如何在ajax完成之前停止对象方法调用

Javascript 如何在ajax完成之前停止对象方法调用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个java脚本对象 function eventTypeObj() { allEventTypes = []; // When the object is created go and get all the event types that can be included in journey or clusters. $.ajax({ url: "/ATOMWebService.svc/GetDisplayEventTypes", dataType:

我有一个java脚本对象

function eventTypeObj() {

allEventTypes = [];

// When the object is created go and get all the event types that can be included in journey or clusters.
$.ajax({
        url: "/ATOMWebService.svc/GetDisplayEventTypes",
        dataType: "json",
        success: function(result) {
            allEventTypes = eval("(" + result.d + ")");
        }
    });


// Returns a list of all the event type IDS.
this.getEventTypeIds = function() {
    var eventTypeIDs = [];
    for (var i = 0; i < allEventTypes.length; i++) {
        eventTypeIDs.push(allEventTypes[i].Id);
    }
    return eventTypeIDs;
};
}
函数eventTypeObj(){
allEventTypes=[];
//当对象被创建时,去获取可以包含在旅程或集群中的所有事件类型。
$.ajax({
url:“/ATOMWebService.svc/GetDisplayEventTypes”,
数据类型:“json”,
成功:功能(结果){
allEventTypes=eval(“+result.d+”);
}
});
//返回所有事件类型ID的列表。
this.getEventTypeIds=函数(){
var eventTypeIDs=[];
对于(变量i=0;i

我想知道是否有办法阻止有人调用eventTypeObj.getEventTypeIds();在构造函数中的ajax调用成功之前,并且allEventTypes数组中没有数据

没有办法阻止有人过早地打电话。如果他们叫得太早,你希望发生什么

如果allEventTypes尚未填充,那么代码现在似乎返回一个空数组。您可以决定空数组是否是正确的结果,或者在调用它太早时是否应该抛出异常,以使调用方完全清楚数据还不可用

您可以为需要该信息的人提供一些帮助程序代码,但可能还不可用。例如,您可以允许他们注册一个回调,该回调将在填充数据后从成功处理程序调用。您可以允许他们查询数据是否可用

如果您不想让呼叫者负责计时,那么就不能提供同步方式来获取此信息。相反,您将只提供用于获取数据的回调机制。如果数据准备就绪,则会立即调用回调。如果数据未准备好,则在ajax函数完成时将调用回调。在任何一种情况下,调用方都必须只处理回调中的数据,而getEventTypeId将不是像现在这样获取数据的普通调用,而是注册回调的调用,该回调将在准备就绪时使用数据调用。这将使调用者不必知道数据何时准备好的实现细节,但会迫使他们使用回调机制的异步特性

this.getEventTypeIds = function(callback) {
    if (allEventTypes.length > 0) {
        // data is ready call the callback with the data now
    } else {
        // store the callback to be called later from the success handler
    }
}

您可以检查eventType数组是否为空,对吗

if(allEventTypes.length == 0)
{
    return;
}
var allowCall=false;
函数eventTypeObj(){
allEventTypes=[];
//当对象被创建时,去获取可以包含在旅程或集群中的所有事件类型。
$.ajax({
url:“/ATOMWebService.svc/GetDisplayEventTypes”,
数据类型:“json”,
成功:功能(结果){
allEventTypes=eval(“+result.d+”);
allowCall=true;
}
});
//返回所有事件类型ID的列表。
this.getEventTypeIds=函数(){
如果(!allowCall)返回;//或弹出消息
var eventTypeIDs=[];
对于(变量i=0;i

或者只需检查allEventTypes是否为空。

这样做会更好(我不能保证这是100%有效的,但概念是合理的):


是的,我也这么想。我确实考虑过返回某种类型的错误代码,但它似乎向其调用者公开了对象的实现细节,并将处理问题的责任转移给了调用者。有没有更好的方法将此类内容封装在ajax/数据对象Java脚本中?我在答案中添加了更多信息,这是我所知道的唯一一种方法,可以免除调用方对数据到达时间的任何责任。+1请jfriend解释。就在他更新答案之前,我发布了回调风格逻辑的完整实现。看一看。
var allowCall = false;
function eventTypeObj() {

allEventTypes = [];

// When the object is created go and get all the event types that can be included in journey or clusters.
$.ajax({
        url: "/ATOMWebService.svc/GetDisplayEventTypes",
        dataType: "json",
        success: function(result) {
            allEventTypes = eval("(" + result.d + ")");
            allowCall = true;
        }
    });


// Returns a list of all the event type IDS.
this.getEventTypeIds = function() {
    if(!allowCall) return;  // or pop up a message
    var eventTypeIDs = [];
    for (var i = 0; i < allEventTypes.length; i++) {
        eventTypeIDs.push(allEventTypes[i].Id);
    }
    return eventTypeIDs;
};
}
function eventTypeObj() {
    this.allEventTypes = [];
    this.hasLoadedEventTypes = false;

    var loadEventTypes = function(cb) {
        $.ajax({
            url: "/ATOMWebService.svc/GetDisplayEventTypes",
            dataType: "json",
            success: function(result) {
                this.allEventTypes = eval("(" + result.d + ")");
                this.hasLoadedEventTypes = true;
                cb();
            }
        });
    };


    this.getEventTypeIds = function(updateEventTypes, callback) {
        var _getEventTypeIds = function() {
            var eventTypeIDs = [];
            for (var i = 0; i < this.allEventTypes.length; i++) {
                eventTypeIDs.push(this.allEventTypes[i].Id);
            }    
            return eventTypeIDs;
        };


        if (!this.hasLoadedEventTypes || updateEventTypes) {
            loadEventTypes(function(){ callback(_getEventTypeIds()); });
        }
        else callback(_getEventTypeIds());
    };
}
var eto = new eventTypeObj();

eto.getEventTypeIds(false, function(eventTypeIdArray) {
   // do stuff with the id array 
});


/*
    somewhere later on you want to get an updated eventTypeId array
    in case the event types have changed.
*/
eto.getEventTypeIds(true, function(eventTypeIdArray) {
    // do stuff with the updated ids
});