且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

又一个jQuery的AJAX调用循环一个没有的网页渲染回采

更新时间:2023-11-21 08:19:16

使用AJAX的异步。你可以,例如,有所有你要处理的表行的数组,占据第一位落阵列,并开始查询它。把code在启动时最后一个完成了一个查询监听器。

  VAR toRequest =新阵列(测试1,测试2,TEST3);
功能doRequest(指数){
  $阿贾克斯({
    网址:'?jsonCall.php数据='+ toRequest [指数]
    异步:真正的,
    成功:功能(数据){
      //做你想做就去做

      如果(指数+ 1< toRequest.length){
        doRequest(索引+ 1);
      }
    }
  });
}
doRequest(0);
 

I have a table of of more then 100 rows.

each row consist of pdf files and their description with Last coloumn of STATUS.

STATUS SHOWS whether pdf files are readable are not.

Once table is loaded in the browser, I am geting each file name from every row of the table and processing it using ajax call. if file is readable, i update the status field of that row as READABLE. processing of each pdf file take 30sec to 1 minute (depending upon the size of file)

I don't want to use async call and send all of 100's of request to my server togather.

when I use async= false. it execute every ajax call one by one, that is what i want to do, but in the same time it stop user to browse the loaded table. so in other words user is kind of stuck untill all of those ajax requests are done. then he can scroll down to reader further information.

I want to allow user to read the web page while in the background i want to execute ajax requests one after another to process pdf files and update its status in every row.

How can I do that.

$('table.tableRecods tr').each(function(){

            fileWithPath = $('#filePath').text();
            $this = $(this);
            $this.find('td.status img.cropStatus').attr('src', 'img/loader.gif') ;           
                $.ajax({
                    url:'jsonCall.php',
                    async:false,
                    data: {file: escape(fileWithPath)},
                    success: function(data){        

                            if(data.status == 'true') {
                                $this.find('td.status img.readStatus').attr('src', 'img/icons/read.png') ;                                  
                            }else if(data.status == 'false'){
                                $this.find('td.status img.readStatus').attr('src', 'img/icons/__read.png') ;

                            }
                    }
                }); 

    });

Use asynchronous ajax. You could, for example, have an array with all the table rows you want to process, take the first one off the array and start a query for it. Put code in the listener that starts the next query when the last one finished.

var toRequest=new Array("test1", "test2", "test3");
function doRequest(index) {
  $.ajax({
    url:'jsonCall.php?data='+toRequest[index],
    async:true,
    success: function(data){        
      //do whatever you want do do

      if (index+1<toRequest.length) {
        doRequest(index+1);
      }
    }
  }); 
}
doRequest(0);