简体   繁体   中英

How do I assign a function to a global variable?

I'm trying to pass the name of a javascript function to a global variable. I have a generic "CallMyWebService" function built around $.ajax(). The function's url, verb, contenttype, etc. are set based on the value of global variables. Immediately before the service function is called, the globals are set and after the globals reset to nothing.

My generic "CallMyWebService" function is here:

function CallMyWebService() { 

    $.ajax(
        {
        url         : varUrl,
        type        : varHttpVerb,
        cache       : varCacheBool,
        data        : varData, 
        contentType : varContentType,
        processdata : varProcessData, 
        dataType    : varDataType, 
        async       : varAsync,
        success     : varOnSuccess,
        error       : varOnError,
        complete    : varOnComplete
        }
    )

}

The problem I'm having is passing the function names to varOnSuccess , varOnError , and varOnComplete . If I have a function named SuccessCallback() and I want the the result of the service call passed to SuccessCallback() , how do I assign it to the varOnSuccess global variable? The variable assignment will take place in another function called SetupServiceCall() . In other words, in the SetupServiceCall() function how should varOnSuccess = be completed?

I would say:

varOnSuccess = SuccessCallback;

Look in the jQuery documentation for details on the parameters that SuccessCallback() will receive.

CallMyWebService should accept those functions as parameters. You should also pass them by reference, instead of by string names.

Don't do a global variable. If you have any sort of parallel calls, this will fail.

Example:

function CallMyWebService(successCallback, errorCallback) {
    $.ajax(
        ...
        success: successCallback,
        error: errorCallback
        ...
    );
}

//calling the service
function MySuccessHandler(data) {
    alert('success');
}

function MyErrorHandler(data) {
    alert('error');
}

CallMyWebService(MySuccessHandler, MyErrorHandler);

you mean like:

var varOnSuccess = null;
function SetupServiceCall() {
   varOnSuccess = SuccessCallback; //note 
}

?

You can simply use the function names without parenthesis in your ajax call, for example:

$.ajax({
...
success: SuccessCallback,
error: ErrorCallback,
...
}); 
window.varOnSuccess = SuccessCallback

should work

However, I think it would be better to pass the variable parts to CallMyWebService as arguments like so:

function CallMyWebService(varSuccess, varError) { 

    $.ajax({
        url         : varUrl,
        type        : varHttpVerb,
        cache       : varCacheBool,
        data        : varData, 
        contentType : varContentType,
        processdata : varProcessData, 
        dataType    : varDataType, 
        async       : varAsync,
        success     : varSuccess,
        error       : varError,
        complete    : varOnComplete
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM