var SHAKE_THRESHOLD = 800;
var last_update = 0;
var x = y = z = last_x = last_y = last_z = 0;
functiondeviceMotionHandler(eventData) {var acceleration = eventData.accelerationIncludingGravity;
var curTime = newDate().getTime();
if ((curTime - last_update) > 500) {
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;
var status = document.getElementById("status");
if (speed > SHAKE_THRESHOLD) {
alert('摇一摇显示');
}
last_x = x;
last_y = y;
last_z = z;
}
}
if(window.DeviceMotionEvent) {
// Mobile browser support motion sensing events
window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
// Mobile browser does not support the motion sensing events
}
于是开始研究起上面的代码不够灵敏的原因,发现:
The device motion event is a superset of the device orientation event; it returns data about the rotation information and also acceleration information about the device. The acceleration data is returned in three axes: x, y and z. They are measured in meters per second squared (m/s^2). Because some devices might not have the hardware to exclude the effect of gravity, the event returns two properties, accelerationIncludingGravity and acceleration, which excludes the effects of gravity, (when this is the case, the acceleration data will be null)
原来HTML5对设备移动有两个加速度相关的数据:
// Grab the acceleration from the results
var acceleration = eventData.acceleration;
info = xyz.replace("X", acceleration.x);
info = info.replace("Y", acceleration.y);
info = info.replace("Z", acceleration.z);
document.getElementById("moAccel").innerHTML = info;
// Grab the acceleration including gravity from the results
acceleration = eventData.accelerationIncludingGravity;
info = xyz.replace("X", acceleration.x);
info = info.replace("Y", acceleration.y);
info = info.replace("Z", acceleration.z);
document.getElementById("moAccelGrav").innerHTML = info;
于是,优化后代码如下:
var SHAKE_THRESHOLD =300,
last_update =0,
x = y = z = last_x = last_y = last_z =0,
function deviceMotionHandler(eventData) {
var acceleration = eventData.accelerationIncludingGravity;
var curTime =newDate().getTime();
if ((curTime - last_update) >500) { //多次移动事件中取两个点的事件间隔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 * 1000);//主要优化点1:原来的计算方式把x、y、z三方向的移动有可能会互相干扰。比如x往真方向,y往负方向,就互相抵消了。var dist = Math.sqrt((x-last_x)*(x-last_x)+(y-last_y)*(y-last_y)+(z-last_y)*(z-last_y))
var speed = dist/diffTime*10000;
//优化点2:摇动速度测试调整,达到最优if (speed > SHAKE_THRESHOLD) { //摇一摇灵敏度
alert('摇一摇显示');
}
last_x = x;
last_y = y;
last_z = z;
}
}
2.页面报WARNING:The devicemotion event is deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS.
function playOrPaused() {
console.log(typeof audio);
console.log(typeof audio.paused);
if (audio.paused) {
audio.play(); //ERROR:Uncaught (in promise) DOMException: The element has no supported sources.
}
}
查阅相关资料发现audio可以支持两种方式设置src,如下:
1. Permitted content: If the element has a src attribute: zero or more
5. WARNING: Deferred long-running timer task(s) to improve scrolling smoothness. See crbug.com/574343.
setTimeout(function () {
shakeOff();
}, n);
在原关闭摇一摇动画效果中,发现有时候debug调试器反馈如山WARNING,通过查资料发现:
The warning is telling you that your timer wasn’t fired on time because it is a long running callback (> 50ms) and the user was/is about to scroll. While your callback is running, Chrome can’t start scrolling the page so this results in “jank”, user input not being handled in a timely manner. To make the experience better for the user, Chrome decided to postpone firing that callback until a time where running it won’t negatively affect the user.
I don’t know the details of what you’re trying to do, but the correct way to do things would be to chunk up your one big callback into smaller batches and spread them out so that any one call will not noticeably delay user actions. You could additionally look at using requestIdleCallback which will call your function only when Chrome is idle and is ideally suited for non-time critical tasks. (However, requestIdleCallback is supported only on Chrome as of now).
我们都晓得java实现线程2种方式,一个是继承Thread,另一个是实现Runnable。
模拟窗口买票,第一例子继承thread,代码如下
package thread;
public class ThreadTest {
public static void main(String[] args) {
Thread1 t1 = new Thread1(
#include<iostream>
using namespace std;
//辅助函数,交换两数之值
template<class T>
void mySwap(T &x, T &y){
T temp = x;
x = y;
y = temp;
}
const int size = 10;
//一、用直接插入排
对日期类型的数据进行序列化和反序列化时,需要考虑如下问题:
1. 序列化时,Date对象序列化的字符串日期格式如何
2. 反序列化时,把日期字符串序列化为Date对象,也需要考虑日期格式问题
3. Date A -> str -> Date B,A和B对象是否equals
默认序列化和反序列化
import com
1. DStream的类说明文档:
/**
* A Discretized Stream (DStream), the basic abstraction in Spark Streaming, is a continuous
* sequence of RDDs (of the same type) representing a continuous st
ReplayingDecoder是FrameDecoder的子类,不熟悉FrameDecoder的,可以先看看
http://bylijinnan.iteye.com/blog/1982618
API说,ReplayingDecoder简化了操作,比如:
FrameDecoder在decode时,需要判断数据是否接收完全:
public class IntegerH
1.js中用正则表达式 过滤特殊字符, 校验所有输入域是否含有特殊符号function stripscript(s) { var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]"
经常在写shell脚本时,会碰到要以另外一个用户来执行相关命令,其方法简单记下:
1、执行单个命令:su - user -c "command"
如:下面命令是以test用户在/data目录下创建test123目录
[root@slave19 /data]# su - test -c "mkdir /data/test123"