Shake the phone----------------------------------------------------------
Init ();
var shake_threshold = 3000;
var last_update = 0;
var x = y = z = last_x = last_y = last_z = 0;
function init () {
if (window. Devicemotionevent) {
Window.addeventlistener (' devicemotion ', Devicemotionhandler, false);
} else {
Alert (' Not a Support mobile event ');
}
}
function Devicemotionhandler (eventData) {
var acceleration = eventdata.accelerationincludinggravity;
var curtime = new Date (). GetTime ();
if ((curtime-last_update) > 100) {
var difftime = curtime-last_update;
Last_update = Curtime;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
var speed = Math.Abs (x + y + z-last_x-last_y-last_z)/difftime * 10000;
if (Speed > Shake_threshold) {
Alert ("Shake");
Media.setattribute ("src", "Http://211.148.5.228:8002/Pages/test/Kalimba.mp3");
Media.load ();
Media.play ();
}
last_x = x;
last_y = y;
Last_z = Z;
}
}
JS to realize the phone shake a shake