Web workers (1/2)
-
Background scripts inside the application
- Run in a isaolated thread
//Create the Worker from a separate file
var worker = new Worker('doWork.js');
//Listen to the ww messages
worker.addEventListener('message', function(e) {
console.log('Worker said: ', e.data);
}, false);
// Send data to our worker and start it.
worker.postMessage('Worker, it's time to run.);
Web workers (2/2)
What can they do
-
Access navigator, location, xhr, appcache, other worker, external scripts with importScripts()
-
No Access DOM, window, document, parent
-
Example
-
html5rocks
Geolocation (1/3)
- Ability to get from a device its position
- It is an asynchronous operation, a callback must be written
- It could be unavailable on the device.
- MDN
- Example
Geolocation current position (2/3)
navigator.geolocation.getCurrentPosition(function(position) {
do_something(position.coords.latitude, position.coords.longitude);
});
- You can get the current position
Geolocation watch position(3/3)
var watchID = navigator.geolocation
.watchPosition(
geo_success,
geo_error,
{
enableHighAccuracy:true,
maximumAge:30000,
timeout:27000
});
- You can watch the user position, a callback is called each time the position change.
- The watch can be cancelled with navigator.geolocation.clearWatch(watchID);
File API (1/2)
<input type="file" id="input" multiple/>
- Add a multiple options
- File can be accessed with JavaScript
File API (2/2)
var inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
var fileList = this.files;
if(this.files.length === 0){return;}
var file = files[0];
console.log(file.name, file.type, file.lastModifiedDate);
}