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 |
瀏覽器相容性
載入中…