-1

I'm trying to get a variable but it's returning undefined. Maybe I'm having an issue with scope or timing?

var pid = getPart(upc);
console.log('pid: '+pid);

addPart(pid);

getPart() is not returning a value but the post is working and getting id. I just can't log pid or use it in addPart() because it seems to not be returning.

function getPart(upc){
    $.post('include/_getpart.php', {upc: upc}, function(data){
        name = data.name;
        id = data.id;

        return id;
    }, "json");
}

For completeness, I will post my entire script.

<script language="javascript">
var success = 0;
var addedArray = [];

function add(form) {
    console.log('form for '+form.name);
    $(':input[type="number"]', form).each(function() {
        if (this.value > 0){
            upc = this.name;
            qty = this.value;
            console.log(upc + ': ' + qty);

            var pid = getPart(upc);
            console.log('pid: '+pid);

            //addPart(pid);
        }
    });
    if (success==1) {
        console.log(addedArray);
    }
}

function getPart(upc){
    $.post('include/_getpart.php', {upc: upc}, function(data){
        name = data.name;
        id = data.id;
        /*
        console.log('name: '+name);
        console.log('product id: '+pid);

        var productArray = new Array(name, qty);
        console.log(productArray);
        addedArray.push(productArray);
        */
        return id;
    }, "json");
}

function addPart(pid){
    if (!pid) {
        console.log('error: product id (pid) was not found');
        return;

    } else {
        $.post('include/_addpart.php', {pid: pid, qty: qty}, function(data) {
            console.log('_addpart.php response: '+data);
        });
    }
}

</script>

All the ajax requests work, I'm wondering if the bit of lag the may make while communicating with the server warrants the setting of said variable to be skipped. If so, should I put a timeout in there, and how.

4

1 回答 1

2

这是由 AJAX 的异步性质和您不能return从回调中引起的。事件的顺序是这样的:

  • getPart()您使用 AJAX 调用请求部件 ( )。
  • 在 AJAX 调用 ( post) 中,您提供了一个匿名函数,我们应该在 AJAX 调用完成时调用该函数。
  • 因为是异步调用,所以JS继续执行,即JS继续到你getPart调用后的那一行,也就是console.log('pid: '+pid);
  • JS 将输出pid: undefined,因为 varpid已声明但未在之前的行中定义。
  • JS 将继续下一步,直到 AJAX 调用的响应返回。
  • 当响应返回时,它将执行之前的匿名函数。
  • 但是,在该函数结束时,它会看到return id;没有任何东西在等待此返回。

你将不得不做这样的事情:

function add(form) {
    console.log('form for '+form.name);
    $(':input[type="number"]', form).each(function() {
        if (this.value > 0){
            ...
            getPart(upc, function(pid){
                // THIS ANONYMOUS FUNCTION WILL BE EXECUTED ONCE THE AJAX CALL RETURNS
                console.log('pid: '+pid);
                //addPart(pid);
            });    
        }
    });
    ...
}

function getPart(upc, callback){
    $.post('include/_getpart.php', {upc: upc}, function(data){
        name = data.name;
        id = data.id;
        // CALLBACK IS THE ANONYMOUS FUNCTION YOU DECLARED ABOVE
        callback(id);
    }, "json");
}

编辑:

如果您只想调用addPart(而不是调用console.log),则不需要匿名函数……那么您可以这样做:

function add(form) {
    console.log('form for '+form.name);
    $(':input[type="number"]', form).each(function() {
        if (this.value > 0){
            ...
            getPart(upc, addPart);    
        }
    });
    ...
}

function getPart(upc, callback){
    $.post('include/_getpart.php', {upc: upc}, function(data){
        name = data.name;
        id = data.id;
        callback(id);
    }, "json");
}
于 2013-09-13T19:37:10.043 回答