gpt4 book ai didi

Javascript - 我的函数地理定位权限 onchange 以几何级数返回值

转载 作者:行者123 更新时间:2023-11-30 20:27:17 24 4
gpt4 key购买 nike

这是我的代码,当用户更改地理权限时应该切换按钮

toogleGeoButtons() 工作正常

但是当启动 onchange 函数时,它甚至会导致 toogleGeoButtons()。所以

function toogleGeoButtons() {
// toggle function
console.log("toggled")
}
geoSettings = {
timeout: 1000 * 10,
maximumAge: 1000 * 60,
enableHighAccuracy: true
}
navigatorPermisstion();
function navigatorPermisstion() {
navigator.permissions.query({
name: 'geolocation'
}).then(function(result) {
if (result.state == 'granted') {
toogleGeoButtons();
// console.log(result.state);
getLocation();
} else if (result.state == 'prompt') {
// console.log(result.state);
navigator.geolocation.getCurrentPosition(revealPosition, positionDenied, geoSettings);
} else if (result.state == 'denied') {
toogleGeoButtons();
// console.log(result.state);
}
result.onchange = function() {
navigatorPermisstion();
}
});
}

function revealPosition(position) {
console.log(position)
}
function positionDenied(positionError) {
console.log(positionError)
}

console screenshot

最佳答案

试试这个:

navigatorPermisstion();
function navigatorPermisstion() {
navigator.permissions.query({
name: 'geolocation'
}).then(function(result) {
if (result.state == 'granted') {
getLocation();
} else if (result.state == 'prompt') {
navigator.geolocation.getCurrentPosition(revealPosition, positionDenied, geoSettings);
}
result.onchange = function() {
toogleGeoButtons();
}
});
}

关于Javascript - 我的函数地理定位权限 onchange 以几何级数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50759443/

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