如何使initMap()函数在执行之间等待
How to make function initMap() wait in between its execution?
我有两个函数
function otherfun(val){
var data = {'latitude': val[0], 'longitude': val[1]};
$.post(URL, data, function(response){
if(response){
// I want to return response from here
}
else{ alert('Error! :('); }
});
}
function initMap() {
var pos = {};
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var output = otherfun([pos.lat,pos.lng]);
alert(output);
// use output's value further
}
函数initMap()初始执行。我将lat和lng的值传递给otherfun()
我想:
- 返回函数otherfun的响应值
- 使initMap()函数等待otherfun()返回并存储在变量输出
- 然后显示带有输出值的警告框。
将initMap拆分为两个函数。初始init和otherfun后调用的回调函数
function otherfun(val) {
var data = {'latitude': val[0], 'longitude': val[1]};
$.post(URL, data, function(response){
if(response){
otherfunCallback(response); // Call a callback function
}
else{ alert('Error! :('); }
});
}
function initMap() {
var pos = {};
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
otherfun([pos.lat,pos.lng]);
}
// The callback function that alert for the output
function otherfunCallback(data) {
// ... extract the data you need
var output = ...;
alert(output);
}
如果您需要存储输出结果,您可以将其保存在一个变量中,而不是区域设置。
相关文章:
- 在周一上午8点至晚上7点之间执行功能
- jquery .each 循环来执行每个数组项,它们之间有延迟
- 验证XHR和标准执行之间的区别
- eval和setTimeout之间的差异执行字符串代码
- 自动执行函数和IIFE之间的区别
- 在两者之间执行 ajax 时粘贴到输入时缺少按键事件
- 为什么 setimeout(function(), 0 ) 和 function() 之间有执行时间差
- 在 rxjs 中执行和订阅之间的区别
- 使用 AJAX 在 jQuery 和 C# 之间传递变量和执行事件
- 如果两个元素之间的距离小于 X,则执行 Y
- 执行套件时脚本之间的超时 - 量角器非角度
- 余烬 了解路由/控制器之间的执行流
- 什么's执行回退时window.jQuery和if(typeof jQuery==“undefined”)之间的
- 在执行CORS时,getJSON和ajax之间的cookie差异
- 如何在nodejs中的两个函数的执行之间放置一个等待
- 在方法执行之间等待一段时间
- 两个语句执行之间所需的延迟
- Jquery函数执行之间的延迟
- 在单击按钮和Javascript方法实际执行之间发生了什么
- 如何使initMap()函数在执行之间等待