等待2个AJAX请求complet(d3.js)whit优化临时等待?(Waiting for 2 AJAX request to complet (d3.js) whit optimize the temp waiting?)
在运行第一个请求的回调函数RUN_ACTIONS()之前我需要2个ajax请求是complet,但是如何等待它(whit优化临时等待)?
function ajax() { d3.tsv( ajaxUrl, function(data) { while (!secondAJAXQueryComplet()); RUN_ACTIONS(HEADER, data); } ); d3.json( ajaxUrl, function(header) { define_Header(header); } ); }I need 2 ajax request are complet before run the callback function RUN_ACTIONS() of the first request, but how waiting it ( whit optimize the temp waiting )?
function ajax() { d3.tsv( ajaxUrl, function(data) { while (!secondAJAXQueryComplet()); RUN_ACTIONS(HEADER, data); } ); d3.json( ajaxUrl, function(header) { define_Header(header); } ); }最满意答案
你没有这样做,你使用一般的回调概念:
function dostuff(callWhenDone) { // ... d3.tsv(ajaxUrl, function callWhenTSVCompletes(data) { // ... d3.json(ajaxUrl, function callWhenJSONCompletes(header) { //... callWhenDone(); // now we're done. }); }); }With this code i run the 2 AJAX Query simultanious and wait the last answer before run my callback function RUN_ACTIONS(). but this method does not seem to me very elegant
function ajax() { var ready=false, datatsv=false; d3.tsv( ajaxUrl, function(data) { if (ready) RUN_ACTIONS(HEADER, data); else { ready=true; datatsv=data; } } ); d3.json( ajaxUrl, function(header) { define_Header(header); if (ready) RUN_ACTIONS(header, datatsv); else { ready=true; } } ); }更多推荐
发布评论