分配值后,JavaScript变量显示为未定义

时间:2019-11-29 21:10:59

标签: javascript jquery asynchronous promise

以下行将Google geocoderService实例,Google地图实例和回调函数传递给texture

calculateOriginCoordinates

其中定义为:

// Convert starting location addresses to coordinates, then calculate midpoint and set map's center
calculateOriginCoordinates(geocoderService, map, findMidpoint);

由于某种原因(大概是因为// Calculate origin coordinates function calculateOriginCoordinates(geocoderService, map, callback) { let start1Coords = convertAddressToCoordinates(geocoderService, document.getElementById("start1").value); let start2Coords = convertAddressToCoordinates(geocoderService, document.getElementById("start2").value); let midpoint = callback(map, start1Coords, start2Coords); return midpoint; } // Find the midpoint of two coordinates function findMidpoint(map, coord1, coord2) { let midpoint = google.maps.geometry.spherical.interpolate(coord1, coord2, 0.5); map.setCenter(midpoint); map.setZoom(7); return midpoint; } 的执行时间大于convertAddressToCoordinates的执行时间),findMidpoint接收到的coord1coord2未定义。

我尝试使用jquery promise和回调函数来解决这个问题,但是都没有用。如何使这些函数按顺序执行并防止未定义的变量?

0 个答案:

没有答案
相关问题