Ajax更新实时JavaScript变量变量、实时、Ajax、JavaScript

2023-09-10 19:21:04 作者:`泛濫の毛疒

我有一个JavaScript函数,需要做数值计算。一些在该计算中使用的号码都存储在数据库中,并且它们将不同取决于如何用户填写在线表格。一旦用户填写表单时,他们会点击计算按钮。此时,在JS的功能,我想用AJAX来获得对应于由用户选择的一些其它的值从数据库中值。

I have a javascript function which needs to do a numerical calculation. Some of the numbers used in this calculation are stored in a database, and they will differ depending on how a user fills out an online form. Once the user fills out the form, they will click the CALCULATE button. At this time, in the JS function, I would like to use ajax to get values from a database that correspond to some other value chosen by the user.

对于一个简单的例子:有3种不同尺寸的T恤,以不同的价格根据每个的大小(存储在数据库中)。用户选择的大小,当他们点击算算,我使用Ajax来获得他们所选择的大小相关联的价格。

For a simple example: there are 3 sizes of t-shirts, with different prices based on each size (stored in database). The user chooses the size, and when they click CALCULATE, I use ajax to get the price associated with the size they chose.

现在的问题是,我想使用AJAX来更新我会在脚本以后使用一些变量。的方式,我想现在就这样做是不行的,在脚本中的变量不被从阿贾克斯更新,我只能从数据库访问的成功里面的值函数Ajax调用。我明白这是因为AJAX异步的性质,这需要一定的时间,在等待从服务器返回的数据,而功能仍继续运行。

The question is, i want to use ajax to update some variables that I will use later on in the script. The way I am trying to do it now doesn't work, the variable in the script doesn't get updated from ajax, I can only access the value from the database inside the success function of the ajax call. I understand this is because ajax in asynchronous by nature, and it takes some time, waiting for the data to be returned from the server, while the function still continues to run

在下面的例子中,AJAX调用返回JSON数据,我有一个调用的函数 isjson(),如果返回的字符串实际上是JSON数据测试。

In the following example, the ajax call returns JSON data, and I have a function called isjson() that tests if the returned string is in fact JSON data.

例如code:

function calculate_cost(){

    var price = 0;
    var size = $('form#tshirt_form [name="size"] option:selected').val();
    $.ajax({
        url:'my_script.php',
        type:'post',
        data:'select=price&table=tshirts.prices&where=size = "' + size + '"',
        success:function(data){
            if(isjson(data)){
                data = $.parseJSON(data);
                data = data[0];
                price = data['price'];
            }else{
                //display error getting data
            }
        }
    });
    //  continue code for calculation
    //  this alert will display "0", but I want the price from the database in there
    alert(price);
    //perhaps do other ajax calls for other bits of data
    //...
    return final_price;
}

有谁知道我能做到这一点,更新与阿贾克斯的实时变量??

Does anyone know how I can accomplish this, updating variables with ajax in real-time??

非常感谢!

**编辑**

感谢大家的帮助,我明白了关于Ajax是异步的。我真的很想回答,我没有继续成功函数中的计算,因为我的实际问题涉及从好几个不同的表多个值。我也希望能够扩大对计算的未来,没有它变得太令人费解。如果这是不可能的,永远比​​我将不得不忍受这一点。 ; - )

Thanks everyone for the help, I understand about ajax being asynchronous. I would really like an answer where I don't have to continue the calculation inside the success function, because my actual problem involves many values from quite a few different tables. I would also like to be able to expand on the calculation in the future without it getting too convoluted. If this is not possible, ever, than i will have to live with that. ;-)

**编辑2 **

OK,我们得到了答案:当然是正确的文档页面的顶部附近,: - /抱歉。该异步属性jQuery的AJAX调用。 http://api.jquery.com/jQuery.ajax/

OK, we got the answer: of course it is right near the top on the docs page, :-/ sorry about that. The async property in jQuery ajax call. http://api.jquery.com/jQuery.ajax/

推荐答案

这是因为 AJAX 默认与之前控制达到异步执行的请求警报(价格); 的请求尚未执行,价格仍持有旧值

That is because ajax executes the request asynchronously by default and before the control reaches alert(price); the request has not yet executed and price still holds the old value.

如果您要执行它同步,那么你可以设置异步为false。

If you want to execute it synchronously then you can set async to false.

$.ajax({
    async: false,
    .....
    .....
});