Geolocation: clearWatch() 方法
clearWatch() 方法是 Geolocation 接口的一部分,用于注销先前通过 Geolocation.watchPosition() 安装的位置/错误监控处理程序。
语法
js
clearWatch(id)
参数
id-
要移除的处理程序是在安装时由
Geolocation.watchPosition()方法返回的 ID 号。
返回值
无(undefined)。
示例
js
let id;
let target;
let options;
function success(pos) {
const crd = pos.coords;
if (target.latitude === crd.latitude && target.longitude === crd.longitude) {
console.log("Congratulations, you've reached the target!");
navigator.geolocation.clearWatch(id);
}
}
function error(err) {
console.error(`ERROR(${err.code}): ${err.message}`);
}
target = {
latitude: 0,
longitude: 0,
};
options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0,
};
id = navigator.geolocation.watchPosition(success, error, options);
规范
| 规范 |
|---|
| Geolocation # clearwatch-method |
浏览器兼容性
加载中…