目录

clearWatch()

描述 (Description)

clearWatch方法取消正在进行的watchPosition调用。 取消后,watchPosition调用将停止检索有关设备当前地理位置的更新。

语法 (Syntax)

以下是此方法的语法 -

clearWatch(watchId);

参数 (Parameters)

这是参数的细节 -

  • watchId - 指定要取消的watchPosition调用的唯一ID。 该值由watchPosition调用返回。

返回值

clearWatch方法不返回值。

例子 (Example)

<!DOCTYPE HTML>
<html>
   <head>
      <script type = "text/javascript">
         var watchID;
         var geoLoc;
         function showLocation(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            alert("Latitude : " + latitude + " Longitude: " + longitude);
         }
         function errorHandler(err) {
            if(err.code == 1) {
               alert("Error: Access is denied!");
            } else if( err.code == 2) {
               alert("Error: Position is unavailable!");
            }
         }
         function getLocationUpdate(){
            if(navigator.geolocation){
               // timeout at 60000 milliseconds (60 seconds)
               var options = {timeout:60000};
               geoLoc = navigator.geolocation;
               watchID = geoLoc.watchPosition(showLocation, errorHandler, options);
            } else {
               alert("Sorry, browser does not support geolocation!");
            }
         }
         function stopWatch() {
            geoLoc.clearWatch(watchID);
         }
      </script>
   </head>
   <body>
      <form>
         <input type = "button" onclick = "getLocationUpdate();" value = "Watch Update"/>
         <input type = "button" onclick = "stopWatch();" value = "Stop Watch"/>
      </form>
   </body>
</html>

这将产生以下结果 -

新页面打开
↑回到顶部↑
WIKI教程 @2018