简体   繁体   English

修改javascript中的局部变量

[英]modify local variable in javascript

the descriptionPercentagesStr variable is not modified in the success queryExt.first. 在成功queryExt.first中不会修改descriptionPercentagesStr变量。

I think it is a syntax error but can not find. 我认为这是语法错误,但找不到。

   var ProductExtendidoClass = Parse.Object.extend("Product");
    var productExtObj = new ProductExtendidoClass();
    var idProduct =this.model.attributes.id;
    var queryExt = new Parse.Query(ProductExtendidoClass);

    descriptionPercentagesStr="inicial";


    queryExt.equalTo("idProduct", idProduct);

    queryExt.first({
      success: function(productExtObj) {

        if (productExtObj!=null)// se tiene el producto
        {
        );
           $("input[name=description_percentages]").val(productExtObj.get("descriptionPercentages"));
           descriptionPercentagesStr=productExtObj.get("descriptionPercentages");

        }
        else
        {
          //alert("lengt es menor o igual a 0");

        }
        // Successfully retrieved the object.
      },
      error: function(error) {
        alert("Error: " + error.code + " " + error.message);
      }
    });
  alert(descriptionPercentagesStr);

Note that the first query function is asynchronous. 请注意,第first查询函数是异步的。 The success and failure functions are its callbacks and are called at a later arbitrary time when the query completes. successfailure函数是其回调,在查询完成后的任意任意时间调用。 They are not called sequentially as with the rest of your code. 不会像其他代码一样顺序调用它们。 So the order of operations in your code is as follows: 因此,代码中的操作顺序如下:

  1. Set descriptionPercentagesStr to "inicial" descriptionPercentagesStr设置为“ inicial”
  2. Query the ProductExtendidoClass for the matching idProduct (results not received yet) ProductExtendidoClass查询匹配的idProduct (结果尚未收到)
  3. Alert descriptionPercentagesStr (returns "inicial") 警报descriptionPercentagesStr PercentagesStr(返回“ inicial”)
  4. Query completes at some later time and descriptionPercentagesStr is set to the result 查询稍后完成,并将descriptionPercentagesStr设置为结果

You can verify this behavior by adding alert(descriptionPercentagesStr); 您可以通过添加alert(descriptionPercentagesStr);来验证此行为alert(descriptionPercentagesStr); into the success callback function. 进入success回调函数。 It should output the result from Parse. 它应该从Parse输出结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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