JavaScript - Workers & Geolocation
Overview
Estimated time: 15–20 minutes
Web Workers allow background processing, and the Geolocation API provides access to the user's location.
Learning Objectives
- Use Web Workers for background tasks.
- Access geolocation data with permission.
Prerequisites
Web Workers
const worker = new Worker('worker.js');
worker.postMessage('start');
worker.onmessage = function(e) {
console.log('Worker said:', e.data);
};
Geolocation API
navigator.geolocation.getCurrentPosition(function(pos) {
console.log(pos.coords.latitude, pos.coords.longitude);
});
Common Pitfalls
- Web Workers can't access the DOM directly.
- Geolocation requires user permission and may fail if denied.
Summary
Web Workers and Geolocation are powerful browser APIs for performance and user experience.