gpt4 book ai didi

javascript - 在 Javascript 中访问变量

转载 作者:行者123 更新时间:2023-11-28 06:17:13 25 4
gpt4 key购买 nike

我有以下代码:

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
else {
alert('Geolocation is not supported by this browser');
}
}
function showPosition(position) {
alert(position.coords.latitude + ' ' + position.coords.longitude);
var Loc = position.coords.latitude + ' ' + position.coords.longitude;
}

我需要能够在函数外部或通过 GetLocation 函数访问 Loc 变量。因为我需要将其绑定(bind)到我正在创建的网格中,以便为异地员工存储上类和下类信息。

这是使用变量 Loc 的代码

getLocation();                      
var rowId=timesheetGrid.uid();
var pos = timesheetGrid.getRowsNum();
timesheetGrid.addRow(rowId,["","","","<?php echo $ActiveWeek; ?>","","","","","","","","","","",Loc],0); //adds a new row with pre filled data
timesheetGrid.selectCell(rowId,0,false,true,true); // Focus on the new row

任何帮助将不胜感激

最佳答案

navigator.geolocation.getCurrentPosition() 工作在异步模式,函数 showPosition(position) 是用于捕获位置的回调函数。所以你需要将所有与位置相关的代码放在这个回调函数中。例如,您可以这样做:

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(whenWeGetPosition);
}
else {
alert('Geolocation is not supported by this browser');
}
}
function whenWeGetPosition(position) {
// your code to use position
var rowId=timesheetGrid.uid();
var pos = timesheetGrid.getRowsNum();
timesheetGrid.addRow(rowId,["","","","<?php echo $ActiveWeek; ?>","","","","","","","","","","",Loc],0); //adds a new row with pre filled data
timesheetGrid.selectCell(rowId,0,false,true,true); // Focus on the new row
}

getLocation();

关于javascript - 在 Javascript 中访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35859424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com