简体   繁体   English

jQuery多个Ajax调用

[英]jQuery multiple ajax calls

While making some ajax calls to two different pages , from which one continuously sends the response while other keeps loading and send's response when finished. 在对两个不同的页面进行ajax调用时,其中一个页面不断发送响应,而另一个页面则继续加载并在完成后发送响应。

This is what i am doing : 这就是我在做什么:

$('.container').html('Please wait... Working : <span id="done">0</span></div>').fadeIn("slow");
$.post('work.php',{'p' : 'proccessImages'},function(data){
    if(data == "success"){
        $('div.container').html("Images processed!").slideDown(3000);
    }
});

Now what am i actually doing is that, when the above call is made, the "Work.php" creates some thumbnails of images which takes time, while there's another script called "done.php" which checks how many images are generated and responds with JSON , when the "Work.php" is done processing it also sends back a "success" message. 现在我真正在做的是,在进行上述调用时,“ Work.php”会创建一些图像缩略图,这会花费一些时间,而另一个名为“ done.php”的脚本会检查生成多少图像并做出响应使用JSON,完成“ Work.php”处理后,它还会发送回“成功”消息。 Here is another function, which updates the span with id "done" with images already done! 这是另一个功能,该功能使用已经完成的图像更新ID为“ done”的跨度!

function repeatJSON(){
    $.getJSON( "http://localhost/done.php", function( data ) {
        var items = [];
        $.each( data, function( key, val ) {
            var currentValue = $("#done").text();
            if(val !== currentValue){
                $("#counter").html("<b>"+val+"</b>").css({"color" : "black"}).hide().fadeIn("slow");
            }
            setTimeout(function(){repeatJSON()},2000);
        });
    });
}

If i execute the above repeating function in the $.post, it won't work and i won't get response from "Work.php" anymore, though script keeps working in the background. 如果我在$ .post中执行上述重复功能,尽管脚本一直在后台运行,但它将无法正常工作,并且也不会再收到“ Work.php”的响应。

Is there any possibility for two multiple calls? 有可能会打两次电话吗? One keeps working other keeps updating ? 一个不断工作,另一个不断更新?

By default AJAX call is asynchronous but jquery AJAX function provides the attribute to make a synchronous call. 默认情况下,AJAX调用是异步的,但是jquery AJAX函数提供了进行同步调用的属性。 You can set the async attribute to false to make as synchronous call and then in the success module of the Work.php you can make another request to the other file which loads the dependent data. 您可以将async属性设置为false以进行同步调用,然后在Work.php的成功模块中,您可以向另一个文件(该文件加载依赖数据)发出另一个请求。

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

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