javascript-for循环内的回调完成后,如何执行函数?
作者:互联网
我在搜索函数中有一个for循环,该函数执行一个在循环内执行回调的回调,并且我想在循环后以及所有回调完成后执行BUILD()函数.我不确定该怎么做,因为循环会在所有回调完成之前完成.回调是向我发送数据的API请求,我想对该数据进行BUILD()处理.
我阅读了deferred的内容,因此我尝试将for循环放入延迟的函数中,然后在’.then(…)’上调用BUILD().但这似乎不起作用-我想我理解错了.
救命?!
请注意,这使用的是Google Maps Places API(search和getDetails).
var types = {
'gym' : 'fitness, gym',
'grocery_or_supermarket': ''
}
function search() {
for (var key in types) {
var request = { ... };
service.search(request, searchCallback);
}
// PROBLEM AREA
BUILD();
}
function searchCallback(results, status) {
for (var i = 0; i < results.length; i++) {
var request = { ... };
service.getDetails(request, detailsCallback);
}
}
function detailsCallback(place, status) {
// add place marker to maps and assign info window and info window event
}
解决方法:
只需对您的代码进行少量修改,就可以实现.
var total = 1337; // Some number
var internal_counter = 0;
var fn_callback = function() {
searchCallback.apply(this, arguments);
if (++internal_counter === total) {
BUILD();
}
};
for (var i=0; i<total; i++) {
service.search(request, fn_callback);
...
说明
首先,我们创建一个局部函数和变量.
>变量是一个计数器,在调用回调时增加.
>该函数传递给异步方法(service.search),该方法调用原始回调.增加计数器后,对照保存迭代总数的变量检查计数器的值.如果它们相等,则调用修整功能(BUILD).
一个复杂的情况:处理嵌套的回调.
var types = { '...' : ' ... ' };
function search() {
var keys = Object.keys(types);
var total = keys.length;
// This counter keeps track of the number of completely finished callbacks
// (search_callback has run AND all of its details_callbacks has run)
var internal_counter = 0;
for (var i=0; i<total; i++) {
var request = { '...' : ' ... ' };
services.search(request, fn_searchCallback);
}
// LOCAL Function declaration (which references `internal_counter`)
function fn_searchCallback(results, status) {
// Create a local counter for the callbacks
// I'm showing another way of using a counter: The opposite way
// Instead of counting the # of finished callbacks, count the number
// of *pending* processes. When this counter reaches zero, we're done.
var local_counter = results.length;
for (var i=0; i<results.length; i++) {
service.getDetails(request, fn_detailsCallback);
}
// Another LOCAL function (which references `local_counter`)
function fn_detailsCallback(result, status) {
// Run the function logic of detailsCallback (from the question)
// " ... add place marker to maps and assign info window ... "
// Reduce the counter of pending detailsCallback calls.
// If it's zero, all detailsCallbacks has run.
if (--local_counter === 0) {
// Increase the "completely finished" counter
// and check if we're finished.
if (++internal_counter === total) {
BUILD();
}
}
} // end of fn_detailsCallback
} // end of fn_searchCallback
}
注释中说明了功能逻辑.我在本节的标题前加上“复杂”,因为该函数使用了嵌套的局部函数和变量.视觉解释:
var types, BUILD;
function search
var keys, total, internal_counter, fn_searchCallback;
function fn_searchCallback
var result, status; // Declared in the formal arguments
var local_counter, i, fn_detailsCallback;
function fn_detailsCallback
var result, status; // Declared in the formal arguments
在上图中,每个缩进级别表示一个新的scope Explanaation on MDN.
例如,当一个函数被调用42次时,将创建42个新的本地范围,它们共享相同的父范围.在范围内,父范围看不到declared variables.尽管父范围内的变量可以由“子”范围内的变量读取和更新,但前提是您不声明具有相同名称的变量.我的答案功能中使用了此功能.
标签:callback,google-maps-api-3,javascript 来源: https://codeday.me/bug/20191201/2078444.html