更新时间:2023-01-17 11:16:46
我想到的第一件事(在这么晚的时间:D)是这样的:
The first thing came my mind (at this late hour :D) is something like this:
在处理程序之外声明一个变量,比如说:
declare a variable outside your handlers, let's say:
var coords = {};
var coords = {};
然后在你的 'handle_geolocation_query' 处理程序中,触发一个事件,让你知道你的位置已经准备好,假设你正在使用 jQuery:
then in your 'handle_geolocation_query' handler, trigger an event that gives you an ideea that your position is ready, assuming you're using jQuery:
$(coords).trigger('positionReady', position);
$(coords).trigger('positionReady', position);
之后,在您需要这些坐标的任何地方,监听您的自定义positionReady"事件,然后完成您的工作:
after that, everywhere you need those coordinates, listen for your custom 'positionReady' event, and do your job:
$(coords).on('positionReady', function(e, position) {控制台.log(位置);});
$(coords).on('positionReady', function(e, position) { console.log(position); });
这将帮助您避免意大利面条式代码",但同时,您需要异步思考",并且可能以您需要的(正确)方式使用回调.
This will help you to avoid 'spaghetti code', but in the same time, you need to 'think' asynchronously, and maybe using callbacks the (right) way you need.