首页 > 解决方案 > 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...
        };
});

标签: javascripttouchtouch-event

解决方案


我找到了这个教程

http://www.javascriptkit.com/javatutors/touchevents3.shtml

这是一种不同的方法(链接是第 3 部分,您可能还需要阅读第 1 部分和第 2 部分)。您可以选择通过的点数组,而不是从事件中选择 touchx 和 y。这应该给你你想要的粒度。

优秀的教程,有关于触摸事件属性的详细信息


推荐阅读