Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
How to call geolocation.getCurrentPosition() without having to give permission every time?

Here's a stripped-down version of a script that displays position information every 5 seconds:

<script>
    var showPositionError = function(error) {
        document.getElementById("message").innerHTML = "Error:  " + error.message
    }
    var showPosition = function(position) {
        console.log(Date(), position.coords.latitude)
    }
    var loop = function() {
        navigator.geolocation.getCurrentPosition(showPosition, showPositionError)
    }
    setInterval(loop, 5*1000)
</script>

It works, but the alert asking me to block or allow the query appears with every iteration.

If I delay answering for a while, it continues iterating, and when I finally do respond all pending queries happen at the same time (the line with 8 repetitions):

  pos2.html:17 Mon Jul 18 2022 16:47:56 GMT-0400 (Eastern Daylight Time) 43.4470912  
  pos2.html:17 Mon Jul 18 2022 16:48:01 GMT-0400 (Eastern Daylight Time) 43.4470912  
  pos2.html:17 Mon Jul 18 2022 16:48:06 GMT-0400 (Eastern Daylight Time) 43.4470912  
8 pos2.html:17 Mon Jul 18 2022 16:48:49 GMT-0400 (Eastern Daylight Time) 43.4470912  
  pos2.html:17 Mon Jul 18 2022 16:48:52 GMT-0400 (Eastern Daylight Time) 43.4470912  
  pos2.html:17 Mon Jul 18 2022 16:48:56 GMT-0400 (Eastern Daylight Time) 43.4470912

This is using Chrome on Ubuntu 20.04.

There's obviously something I'm not understanding about how these mechanisms work.

What do I need to do so that it will ask for permission only the first time?

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!