HTML5 Game - Registering a Global Timeout for GEO api

Description

Registering a Global Timeout for GEO api

Demo

ResultView the demo in separate window

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Canvas Demo</title> 
    </head> 
    <body> 
      <h1>Geolocation Example</h1> 
      <div id="locationValues"> 
      </div> 
      <div id="error"> 
      </div> 
      <script> 
let globalTimeout = null; //from  w  ww.  j ava2  s .co m
  
function successCallback(position) { 
  if (globalTimeout == null) { 
    return; 
  } else { 
    clearTimeout(globalTimeout); 
  } 
  let locationValues = document.getElementById('locationValues'); 
  
  let myUl = document.createElement('ul'); 
  
  for (let geoValue in position.coords) { 
    let newItem = document.createElement('li'); 
    newItem.innerHTML = geoValue + ' : ' + position.coords[geoValue]; 
    myUl.appendChild(newItem);  
  } 
  
  newItem = document.createElement('li'); 
  newItem.innerHTML = 'timestamp : ' + position.timestamp; 
  myUl.appendChild(newItem); 
  
  locationValues.appendChild(myUl); 
} 
  
function errorCallback(error) { 
  if (globalTimeout == null) { 
    return; 
  } else { 
    clearTimeout(globalTimeout); 
  } 
  let myError = document.getElementById('error'); 
  let myParagraph = document.createElement('p'); 
  myParagraph.innerHTML = 'Error code ' + error.code + '\n' + error.message; 
  myError.appendChild(myParagraph); 
} 
function globalTimeoutCallback() { 
  console.log('Error: GPS permission not given, exiting application.'); 
  globalTimeout = null; 
} 
  
navigator.geolocation.getCurrentPosition(successCallback, errorCallback); 
  
globalTimeout = setTimeout(globalTimeoutCallback.bind(this), 5000); 
      </script> 
    </body> 
</html>

Related Topic