Dynamically change the number of calls to 'defer' in queue.js

My web page allows the user to display multiple time series on a chart, and I use queue.js to navigate and get this data asynchronously, for example:

queue()
.defer(d3.json, "api/TransactionCost/?marketCode=" + marketCode1)
.defer(d3.json, "api/TransactionCost/?marketCode=" + marketCode2)
.defer(d3.json, "api/TransactionCost/?marketCode=" + marketCode3)
.await(onDataLoaded);

function onDataLoaded(error, json1, json2, json3) {
  // plot the 3 timeseries
}

I want the user to be able to request additional lines if they wish, which would mean that I need to make additional “defer” calls. I would like to know how to dynamically add additional “deferred” calls (if possible), as well as how to create the onDataLoaded function so that it can handle a variable number of parameters.

+4
source share
1 answer

, 'defer' ( )

defer:

var q = queue();
for (/* each file */)
    q = q.defer(d3.json, filename);
q.await(onDataLoaded);

onDataLoaded, .

arguments .

function onDataLoaded(error) {
    if (!error) {
        // Either simply loop them:
        for (var i=1; i<arguments.length; i++)arguments[i] …
        // or slice them into an array:
        var jsons = Array.prototype.slice.call(arguments, 1);
    } else { … }
}
+9

Source: https://habr.com/ru/post/1526236/


All Articles