Is there conditional looping with $.each function in jQuery

前端 未结 3 1891
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 13:36

I have a query about jQuery\'s $.each method. Below is my ajax which is working quite well:

$.ajax({
    url:\'js/people-json.js\',
    type:\'post\         


        
3条回答
  •  再見小時候
    2021-01-29 14:29

    Sticking with jQuery, the simplest approach is to loop through all items in data.names, up to the last item in the batch, rejecting those items prior to the start of the batch.

    var nameObj = {
        index: 0,
        batchSize: 200
    };
    function nextNamesBatch() {
        $.each(data.names, function(i, data) {
            if(i < nameObj.index) return true;//==continue
            if(i >= nameObj.index + nameObj.batchSize) return false;//==break
            console.log(data);
        });
        nameObj.index += nameObj.batchSize;
    }
    

    Yes, there's an increasing efficiency issue as the batches progress but I believe that, for moderate size arrays, the overhead will be less than that in the alternative jQuery approach, namely to throttle data.names (or a clone of it) prior to $.each(...).

提交回复
热议问题