等待javascript函数返回

等待javascript函数返回,javascript,ajax,synchronization,return,Javascript,Ajax,Synchronization,Return,我正在做一个对服务器进行同步ajax调用的函数。 问题是,当我调用函数时,main不会等待数据传播 我更好地解释自己: 我需要从函数返回一些数据: var firstdate = getLastPaycheck(); 此功能是: function getLastPaycheck() { let returnValue = sessionStorage.getItem('last_paycheck'); if (returnValue == "" || returnValue

我正在做一个对服务器进行同步ajax调用的函数。 问题是,当我调用函数时,main不会等待数据传播

我更好地解释自己:

我需要从函数返回一些数据:

var firstdate = getLastPaycheck();
此功能是:

function getLastPaycheck() {
    let returnValue  = sessionStorage.getItem('last_paycheck');

    if (returnValue == "" || returnValue == null) {
        const message = { "cookie": getCookie("userCookie") };
        var xhr = new XMLHttpRequest();
        xhr.addEventListener('load', complete, false);
        xhr.open("POST", '/getLastPaycheck', false);
        xhr.setRequestHeader("Content-type", "application/json");
        xhr.send(JSON.stringify(message));

        function complete(e) {//Call a function when the state changes.
                const response = JSON.parse(xhr.responseText);
                console.log("Value taken from the server");
                returnValue = moment(response.value[0].last_paycheck).format(getDateFormat("iso"));
                return returnValue;
            console.log("Returned value");
            } // if readyStatus = 200;

    } else {
        console.log("Value taken from the session");
        return returnValue;
    }
}
问题在于,当从服务器获取数据时,“firstdate”的值总是未定义的

有人能告诉我怎么做吗

多谢各位。我很感激。

您可以使用,然后只需拨打电话

var firstdate=wait getLastPaycheck()

您可以使用,然后只需调用


var firstdate=wait getLastPaycheck()

您也可以使用回调执行此操作:

    function getLastPaycheck(callback) {
        let returnValue  = sessionStorage.getItem('last_paycheck');

        if (returnValue == "" || returnValue == null) {
            const message = { "cookie": getCookie("userCookie") };
            var xhr = new XMLHttpRequest();
            xhr.addEventListener('load', complete, false);
            xhr.open("POST", '/getLastPaycheck', false);
            xhr.setRequestHeader("Content-type", "application/json");
            xhr.send(JSON.stringify(message));

            function complete(e) {//Call a function when the state changes.
                    const response = JSON.parse(xhr.responseText);
                    console.log("Value taken from the server");
                    returnValue = moment(response.value[0].last_paycheck).format(getDateFormat("iso"));
                    callback(returnValue);
                console.log("Returned value");
                } // if readyStatus = 200;

        } else {
            console.log("Value taken from the session");
            callback(returnValue);
        }
    }

    getLastPaycheck(function(data) {
      // data is accessible here
    });

您也可以使用回调来执行此操作:

    function getLastPaycheck(callback) {
        let returnValue  = sessionStorage.getItem('last_paycheck');

        if (returnValue == "" || returnValue == null) {
            const message = { "cookie": getCookie("userCookie") };
            var xhr = new XMLHttpRequest();
            xhr.addEventListener('load', complete, false);
            xhr.open("POST", '/getLastPaycheck', false);
            xhr.setRequestHeader("Content-type", "application/json");
            xhr.send(JSON.stringify(message));

            function complete(e) {//Call a function when the state changes.
                    const response = JSON.parse(xhr.responseText);
                    console.log("Value taken from the server");
                    returnValue = moment(response.value[0].last_paycheck).format(getDateFormat("iso"));
                    callback(returnValue);
                console.log("Returned value");
                } // if readyStatus = 200;

        } else {
            console.log("Value taken from the session");
            callback(returnValue);
        }
    }

    getLastPaycheck(function(data) {
      // data is accessible here
    });

您的连接可能是同步的,但您仍然依赖于事件,而事实并非如此。此外,回调函数内的return语句不会触发外部函数的返回。您的连接可能是同步的,但仍然依赖于事件,而这些事件不是同步的。此外,回调函数内的return语句不会触发外部函数的返回。您好,谢谢,但似乎不起作用:/I did getLastPaycheck(函数(数据){startdate.value=data;});但它似乎不起作用。这是我做的函数:函数getLastPaycheck(callback){let returnValue=sessionStorage.getItem('last_paycheck');…这可能是因为它没有进入if语句。在else中,也做callback(returnValue)。检查编辑。好吧!它工作了,我忘了将“return”改为“callback”!非常感谢,我有一个brew!嗨,谢谢,但似乎不起作用:/I做了getLastPaycheck(函数(数据){startdate.value=data;});但似乎不起作用。这是我做的函数:函数getLastPaycheck(回调){let returnValue=sessionStorage.getItem('last_paycheck'));嗯,这可能是因为它没有进入if语句。在else语句中,也要执行callback(returnValue)。检查编辑。好吧!它工作了,我忘了将“return”更改为“callback”!非常感谢,我有一个brew!