jQuery .load inside .each

Go To StackoverFlow.com

2

tried to search for this, although I am not even sure what I am searching for. My problem is this:

$.getJSON('process.countfiles.php?action=getfiles', function(data) {
    $.each(data, function(name, value){
        $("#displayfile").load("process.files.php?file="+value, function(){
        });
    });
});

OK, I am having a script (process.countfiles.php) with 2 functions, 1 - count the files and build a file list in a .dat file. Then number 2 is open the .dat file and process the files individually.

The first parts works great, so let move to the second part... I have the data being passed from PHP like this:

PHP Code:

$fh = fopen($dirdatafn, "r"); 
    while (!feof($fh)){ 
        $data_file_line[] .= fgets($fh); 
    } 
    @fclose ($fh); 
    echo json_encode($data_file_line); 

Now back to the JS (jQuery is the framework I am using for this) data is the array of $data_file_line from the PHP, it gets the data with no problem, then it should start separating them with the .each

The problem is, it will try loading all 7,000+ at the same time and send them all to the process.files.php, when I would like it to handle one at a time.

Any help would be appreciated.

Thank you, Jeff

2012-04-04 22:38
by SecureLive
Why not use setTimeout to time a function that crawles you data - rcdmk 2012-04-04 22:41
You haven't really described what you want the behavior to be. We follow that you call process.countfiles.php and get a bunch of data back and that one item from that data can be sent to process.files.php. You can do that with one. What do you want to do with the 7000 responses you get? Load the next one every 30 seconds? Load them each into different divs on the web page? Make a giant list of all the results? What do you want to do with all the process.files.php responses you can get and how to you want them separated in time - jfriend00 2012-04-04 22:52
>
  • Put all of the files and directories on the server in a data file 1 line at a time.
  • Get each line from the data file and call each file and open them line by line looking for key phrases. (For example malicious hacks)
  • process the files looking for the hacks (This is not in this bit of code)
  • Output all of the files, line numbers, along with the line of code found that contains the hack.
  • Allow the admin to open the file and edit out the line or one click remove line, or delete file (if it is a total hacked file like a shell or something)
  • - SecureLive 2012-04-05 01:48


    1

    You can tell jQuery to make your calls to "process.files.php?file=" synchronously if you use the more general $.ajax() method instead of $("selector").load();

    replace

    $("#displayfile").load("process.files.php?file="+value, function(){
            });
    

    with

    $.ajax("process.files.php?file="+value, {
            async: false, // this is true by default
            success: function(data) {
                $("#displayfile").html(data); // this is what you were doing by calling $.load
                // do other stuff
            }
         });
    
    2012-04-04 22:55
    by John W
    Overall this worked, the only issue with it is it slowed the processing from 32 seconds @ 7,400 files to 42 minutes @ 7,400 files. So I am rethinking my strategy on this system. THANK YOU for your hel - SecureLive 2012-04-05 04:18


    1

    Providing you JSON returns an array you can do this (i have not tested it). Basically to process each file indiviudally you need to wait till the success callback fires before your next file is processed:

        var data_arr;
        $(function() {
            $.getJSON('process.countfiles.php?action=getfiles', function(data) {
                data_arr = data;
                requestFiles(data_arr[0])
            });
            function requestFiles(i) {
    
                $("#displayfile").load("process.files.php?file=" + data_arr[i], function() {
                    if(i < data_arr.length) {
                        requestFiles(i + 1);
                    }
                });
            }
    
        })
    
    2012-04-04 22:59
    by Michal
    I was unable to get this method to work perfectly, although in theory I see your method of madness here... Thank yo - SecureLive 2012-04-05 04:15
    Ads