-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshake copia.js
More file actions
41 lines (34 loc) · 1.22 KB
/
shake copia.js
File metadata and controls
41 lines (34 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var shakeEvent = function() {
alert('shaked');
};
var shakeSuccess = function(coords) {
var max = 2;
if (Math.abs(coords.x) > max || Math.abs(coords.y) > max || Math.abs(coords.z) > max) {
console.log('shaked it');
//disable shaking for 10 seconds to prevent too many updates
stopWatchingShake();//prevent endless looping: store interval
sessionStorage.intervalId=setInterval(startWatchingShake, 10000);
//do whatever you want on "shake" here
shakeEvent();
}
};
var shakeError = function(){
console.log('shake error');
};
var stopWatchingShake = function(){
console.log('stop watching shake');
//clear watching from sessionStorage reference
navigator.accelerometer.clearWatch(sessionStorage.watchId);
};
var startWatchingShake = function(){
console.log('start watching shake');
//prevent endless loop (kill all intervals)
if(sessionStorage.intervalId){
clearInterval(sessionStorage.intervalId);
}
var options = {};
options.frequency = 100;
//start watching acceleration (save reference in session storage)
sessionStorage.watchId = navigator.accelerometer.watchAcceleration(shakeSuccess,
shakeError, options);
};