简体   繁体   English

jQuery的工作太快?

[英]JQuery works too fast?

I'm pretty new to JQuery and there's a tiny issue with my code. 我对JQuery很陌生,我的代码有一个小问题。 I hope either one of you could help me out. 我希望你们中的任何一个都能帮助我。

I use an external PHP file to load certain HTML data into my main file. 我使用外部PHP文件将某些HTML数据加载到主文件中。 Now there's another external PHP file with updates the very same data. 现在,还有另一个外部PHP文件,用于更新完全相同的数据。 I'm using a JQuery function to check the value of the field I'd like to update, and in case it is not empty I call the update file. 我正在使用JQuery函数检查我想更新的字段的值,并且如果它不为空,我会调用更新文件。 Right after I reload the DIV I'm working on and post into it the new data (using the external loading data file). 在重新加载DIV之后,我正在处理并将新数据发布到其中(使用外部加载数据文件)。

Now the problem is, I believe my JQuery function /sometimes/ doesn't wait for the updating file to finish and instantly reads the new content from the second file, without waiting for the content to get actually updates. 现在的问题是,我相信我的JQuery函数/ sometimes /不会等待更新文件完成,而是立即从第二个文件读取新内容,而不必等待内容实际更新。 This happens every now and then, but not constantly. 这种情况有时会发生,但不会经常发生。

This is my JQuery function: 这是我的JQuery函数:

$("#updateAdminMessage").live("click", function() {
        if($("#adminMessage").val() != ""){
            $.post("/includes/script_files/updateAdminMessage.php", { adminMessage: $("#adminMessage").val() } )
            $("#div_adminMessage").hide();
            $('#div_adminMessage').load('/includes/script_files/loadAdminMessageClass.php');
            $("#div_adminMessage").fadeIn();
        }
        else{
            alert('do not leave this field blank');
        }
    });

In case you're wondering, the reading file simply prints some HTML code after reading the needed data from my database, and the updating file updates the very same data. 如果您想知道,读取文件在从我的数据库中读取所需的数据后仅打印一些HTML代码,而更新文件将更新相同的数据。

Is there anything wrong with my JQuery code? 我的JQuery代码有什么问题吗? Because frankly I believe both of my PHP reading&updating files work just fine. 因为坦率地说,我相信我的两个PHP读取和更新文件都可以正常工作。

The problem is that post is an asychronous operation, so it will execute and return immediately and while waiting for that request to finish, the load function will be executed. 问题在于post是一个异步操作,因此它将执行并立即返回,并且在等待该请求完成时,将执行load函数。

The right way to do it would be to use a callback: 正确的方法是使用回调:

$.post("/includes/script_files/updateAdminMessage.php", { 
    adminMessage:    $("#adminMessage").val() }, 
    function(){
        $("#div_adminMessage").hide();
        $('#div_adminMessage').load('/includes/script_files/loadAdminMessageClass.php');
        $("#div_adminMessage").fadeIn();
})

You can use the callback success function of post to initiate the remaining load: 您可以使用post的回调成功函数来初始化剩余负载:

$("#updateAdminMessage").live("click", function() {
        if($("#adminMessage").val() != ""){
            $.post("/includes/script_files/updateAdminMessage.php", { adminMessage: $("#adminMessage").val() }, success: function(){
                $("#div_adminMessage").hide();
                $('#div_adminMessage').load('/includes/script_files/loadAdminMessageClass.php');
                $("#div_adminMessage").fadeIn();
            })
        }
        else{
            alert('do not leave this field blank');
        }
    });

You're right, jQuery.post() isn't a blocking function, however its third parameter is the success function, so you can put whatever you want to happen "after" the post in there: 没错, jQuery.post()不是阻止函数,但是它的第三个参数是成功函数,因此您可以将想要发生的任何事情放在帖子的“后面”:

        $.post("/includes/script_files/updateAdminMessage.php", { adminMessage: $("#adminMessage").val() }, function(data,status,xhr){
           $("#div_adminMessage").hide();
           $('#div_adminMessage').load('/includes/script_files/loadAdminMessageClass.php');
           $("#div_adminMessage").fadeIn(); });

That is what you call a race condition. 这就是您所说的竞赛条件。 I assume you have some database in the background, so the first php script and the hide() are racing against each other. 我假设您在后台有一些数据库,因此第一个php脚本和hide()相互竞争。 Solutions are 解决方案是

  1. Add some sleep between post and load 在发布和加载之间增加一些睡眠
  2. Use a success callback , that will wait for post() to finish. 使用成功回调 ,它将等待post()完成。

Welcome to the wonderfull world of asynchronous. 欢迎来到异步的美好世界。

This needs to go in to the success callback handler of "post": 这需要进入“ post”的成功回调处理程序:

$("#div_adminMessage").hide();
$('#div_adminMessage').load('/includes/script_files/loadAdminMessageClass.php');
$("#div_adminMessage").fadeIn();


jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

looks like this 看起来像这样

$.ajax({
    type: "POST",
    url: url,
    data: data,
    success: function (data, textStatus, jqXHR) {

        $("#div_adminMessage").hide();
        $('#div_adminMessage').load('/includes/script_files/loadAdminMessageClass.php');
        $("#div_adminMessage").fadeIn();

    },
    dataType: dataType
});

etc. probably the "fadeIn" needs to go in the next callback 等等。“ fadeIn”可能需要进入下一个回调

you may force jQuery to wait/block until the request has been completed, but thats breaking the concept behind AJAX ("A" stands for asynchronous): 您可能会强制jQuery等待/阻止,直到请求完成为止,但是那打破了AJAX背后的概念(“ A”代表异步):

...
async: false,
data: ....

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

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