简体   繁体   中英

How to load multiple files successively with ajax?

I have a large text file with some 2 million records, when I load it with ajax it takes a lot of time, I would like to split it and load the split files successively.

连续的ajax加载

I use this code to load that single large file that I want to split

$.ajax({
    url: "singleLargeFile.csv",
    async: true,
    success: function () {
        process();
        },
    error: function(e) {
        var errorMsg = e? (e.status + ' ' + e.statusText) : "";
        console.log(errorMsg);
        },
    dataType: "text"
    });

Is there a way to load multiple files and each one after being loaded triggers both its callback and the loading of the next file ?

Thanks

Actually there is a function in jquery $.when() I used it like this :

var loadFile1 = $.ajax({
    url: "smallerFile1.csv",
    async: true,
    success: function () {
        process1();
        },
    error: function(e) {
        var errorMsg = e? (e.status + ' ' + e.statusText) : "";
        console.log(errorMsg);
        },
    dataType: "text"
   });


$.when(loadFile1).then(function(){
    var loadFile2 = $.ajax({
        url: "smallerFile2.csv",
        async: true,
        success: function () {
            process2();
            },
        error: function(e) {
            var errorMsg = e? (e.status + ' ' + e.statusText) : "";
            console.log(errorMsg);
            },
        dataType: "text"
        });
    });

Now the process1 is executed when the first file is loaded and then the second load is triggered and so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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