jQuery - Can threads/asynchronous be done?

后端 未结 2 1355
耶瑟儿~
耶瑟儿~ 2020-11-27 08:49

I\'m currently using AJAX with Django Framework.

I can pass asynchronous POST/GET to Django, and let it return a

2条回答
  •  天涯浪人
    2020-11-27 09:31

    Try processing retrieved data incrementally.

    At piece below , elements generated in blocks of 250 , primarily utilizing jQuery deferred.notify() and deferred.progress().

    When all 10,000 items processed , the deferred object is resolved with the collection of 10,000 elements. The elements are then added to document at single call to .html() within deferred.then()'s .done() callback ; .fail() callback cast as null .

    Alternatively , could append elements to the document in blocks of 250 , within deferred.progress callback ; instead of at the single call within deferred.done , which occurs upon completion of the entire task.

    setTimeout is utilized to prevent "freeze the web browser" condition .

    $(function() {
    // 10k items 
    var arr = $.map(new Array(10000), function(v, k) {
      return v === undefined ? k : null
    });
      
    var len = arr.length;
    var dfd = new $.Deferred();
    // collection of items processed at `for` loop in blocks of 250
    var fragment = [];
    var redraw = function() {
      for (i = 0 ; i < 250; i++)
        {
            // configurationVariableChart.row.add(
            // $(
            fragment.push('' +
                    '' + arr[i] + '' +
                '')
            // )[0]);
        };
        arr.splice(0, 250);
        console.log(fragment, arr, arr.length);
        return dfd.notify([arr, fragment])
    };
    
    $.when(redraw())
    // `done` callbacks
    .then(function(data) {
      $("#results").html(data.join(","));
      delete fragment;
    }
      // `fail` callbacks      
     , null
      // `progress` callbacks
     , function(data) {
      // log ,  display `progress` of tasks
         console.log(data);
         $("progress").val(data[1].length);
         $("output:first").text(Math.floor(data[1].length / 100) + "%");
         $("output:last").text(data[1].length +" of "+ len + " items processed");
         $("#results").html("processing data...");
         if (data[0].length) {
             var s = setTimeout(function() {
                 redraw()
             }, 100)
         } else {
           clearTimeout(s);
           dfd.resolve(data[1]);
         }
    })
    })
    
    
    
    jsfiddle http://jsfiddle.net/guest271314/ess28zLh/

提交回复
热议问题