javascript - Javascript touch-event:如何增加触发频率?
问题描述
有一个由“touchmove”触发并执行函数的事件监听器。一切都很好,很整洁。
但是,据我了解,该功能每(可以说)500ms 触发一次,这是非常慢的频率。
“touchmove”如何每 300 毫秒或 100 毫秒触发一次功能?
//this event listener is very slow
canvas.addEventListener('touchmove', draw_fn, false);
function draw_fn(e) {
getTouchPos(e);
drawDot(canvas,touchX,touchY,12);
event.preventDefault();
}
// tried to register touchmove with a bigger frequency
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimaitonFrame ||
function (callback) {
window.setTimeout(callback, 20); //or 200 or 100 or 50 or...
};
});
解决方案
我找到了这个教程
http://www.javascriptkit.com/javatutors/touchevents3.shtml
这是一种不同的方法(链接是第 3 部分,您可能还需要阅读第 1 部分和第 2 部分)。您可以选择通过的点数组,而不是从事件中选择 touchx 和 y。这应该给你你想要的粒度。
优秀的教程,有关于触摸事件属性的详细信息
推荐阅读
- sql-server - 两列的 SQL 连接
- mongodb - 类似索引的 Mongo 索引策略
- python - numpy的问题点积不起作用
- linux - bash 错误突然出现在基本操作系统中
- xslt - 带有method =“text”的xslt复制元素
- angular - 如何通过缓存在 Angular 9 中的组件实例之间维护变量的值?
- ruby-on-rails - Ruby:数据没有引号,但 CSV.parse 引发 CSV::MalformedCSVError: Illegal quoting in line x
- javascript - 如何在 JavaScript 中创建健康评分计算器?
- yolo - yolo暗网框架CUDA错误:无效设备符号暗网:./src/cuda.c:36:check_error:断言“0”失败
- python - 如何打印所有值都在一个数字之间的行数